Welcome Guest, Not a member yet? Register   Sign In
Passing extra data to callback.
#1

[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!
#2

[eluser]Unknown[/eluser]
BUMP
#3

[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;
    }
}
#4

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

https://bitbucket.org/ellislab/codeignit...r-does-not




Theme © iAndrew 2016 - Forum software by © MyBB