CodeIgniter Forums
Passing extra data to callback. - 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 extra data to callback. (/showthread.php?tid=38257)



Passing extra data to callback. - El Forum - 02-03-2011

[eluser]Unknown[/eluser]
I found a post related to CI 1.63 that showed how to pass extra data to a call back using the following code.

Code:
$this->form_validation->set_rules('username', 'Username', 'callback_username_check['.$id.']');

and then you can setup your callback function to receive more than 1 argument

Code:
function username_check($str, $id) {}

However this doesn't seem to be in the documentation. Is this bad practice or deprecated?

I have seen other posts that suggest that you should use variables or post/get data within your callback in order to use data other than the form field passed however this does break encapsulation.

Thanks!


Passing extra data to callback. - El Forum - 02-08-2011

[eluser]Unknown[/eluser]
BUMP


Passing extra data to callback. - El Forum - 02-10-2011

[eluser]sckarlin[/eluser]
I upgraded some code to CI 2.0.0 and found that parameters to callbacks didn't work the way they used to.

I found that by commenting out line 494 of Form_validation.php, my $rules weren't clobbered by the preg_match in line 492. I'm still in the early stages of experimenting with this so I don't know if this breaks other behavior.

Code:
// If the field is blank, but NOT required, no further tests are necessary
$callback = FALSE;
if ( ! in_array('required', $rules) AND is_null($postdata))
{
    // Before we bail out, does the rule contain a callback?
    if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
    {
        $callback = TRUE;
        //$rules = (array('1' => $match[1]));  // why??
    }
    else
    {
        return;
    }
}



Passing extra data to callback. - El Forum - 02-10-2011

[eluser]sckarlin[/eluser]
Looks like someone else has noticed this, too:

https://bitbucket.org/ellislab/codeigniter/issue/139/validation-callback-parameter-does-not