CodeIgniter Forums
Passing variables to custom validation callbacks - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Passing variables to custom validation callbacks (/showthread.php?tid=7138)



Passing variables to custom validation callbacks - El Forum - 03-26-2008

[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


Passing variables to custom validation callbacks - El Forum - 03-26-2008

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


Passing variables to custom validation callbacks - El Forum - 03-26-2008

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

Cheers!


Passing variables to custom validation callbacks - El Forum - 04-14-2008

[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