Welcome Guest, Not a member yet? Register   Sign In
Passing variables to custom validation callbacks
#1

[eluser]jamesf[/eluser]
Just a quick question, couldn't seem to find anything about it when I searched...

Is there a way of passing variables to your custom validation callbacks e.g.

Code:
$rules['username'] = "callback__valid_username($user_id)";

The project I'm working on allows 'admins' to create and edit 'users', when adding a new user I've created a function that will check the database for a duplicate username and this is working ok.

The problem comes when I'm editing a 'user', I need to do a check on the username whilst knowing the id of the user in question.

Regards,

James
#2

[eluser]xwero[/eluser]
you have to use square brackets
#3

[eluser]jamesf[/eluser]
Code:
$rules['username'] = "callback__valid_username[{$id}]";

Cheers!
#4

[eluser]captainbeef[/eluser]
Hi,
I was looking around for advice on exactly the same scenario. So thanks for the advice.

I just thought I'd note that any vars specifically passed into the custom validation function are additional to the variable being validated. So, for validating $username:

Code:
$rules['username'] = "required|callback_check_username[{$id}]";
.
.
function check_username($username, $id){
    
}

cheers




Theme © iAndrew 2016 - Forum software by © MyBB