Welcome Guest, Not a member yet? Register   Sign In
form_validation library | Callbacks, Rules And MORE :D
#1

[eluser]Ricardo Machado[/eluser]
Today I was developing a part of my client's website and started doing the form.

As I was doin'it I defined rules (for form_validation library) for those form fields, being one of them 'tricky' for your (our?) form_validation library.

The rule was:

"trim|callback__MyFunction1|callback__MyFunction2|xss_clean"

You'll probably ask first: "Why 2 callbacks and not just one?!"

Because I wanted to have 2 different errors since the first MyFunction checks its format and the second MyFunction checks its database existence...

Everything was right, running ok... Until I tested it with a correct value (as "correct" I mean WELL FORMATTED) that already existed in the database (the MyFunction2 returns FALSE if exists Big Grin )

Well... guess what... it didn't run the function... yeah I know you would probably say "Man you suck... Want a PHP E-book?"... Well I have one don't need thanks Smile ehehe

But, as curious as I am, started debugging and found out the whole problem was in the form_validation class...

And you'll probably gonna think "Oh really...? Undecided Where exactly?"

Here (line 610 @ Form_validation.php):
Code:
if ( ! in_array('required', $rules, TRUE) AND ($result !== FALSE))
{
    return;
}

As you can see in the rule (at the beginning of the post) you'll see that my rule doesn't have a "required" parameter defined...
You also will be able to see that I sent a valid value to MyFunction1 so, it will return TRUE...

So what will happen? The IF condition will be true and the function _execute will RETURN;

That will kick me out of the object validation, without check/run MyFunction2 and xss_clean rules Smile

I corrected that by changing return; to continue;

Therefore:
Code:
if ( ! in_array('required', $rules, TRUE) AND ($result !== FALSE))
{
    continue;
}

Hope that helps....

Hugs, and hope you don't mind about my enthusiastic way of posting... just to make the post a little bit less boring Tongue (just a little tiny bit)
#2

[eluser]Bogdan Tanase[/eluser]
Hi,

I've also encountered this issue and opened a bug report quite a while ago. It's status is still new, whatever that means...
#3

[eluser]Ricardo Machado[/eluser]
Didn't know... didn't put much effort on the search too ... But ok... thanks Smile
#4

[eluser]xwero[/eluser]
get the form_validation file from SVN and check if the error is still there because i can't find your line in the whole file.
#5

[eluser]voidstar[/eluser]
I also ran into this problem today, but the symptom was different. I found that the callback's parameter was being stripped when an optional field was not entered.

I think both problems stem from the following lines in Form_validation.php, which I just retrieved from SVN (1599).

// 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]));
}
else
{
return;
}
}

Only one callback is inserted into the new "$rules" array with no parameter (\w+).

I fixed this problem by replacing the inner if/else with the following:

$new_rules = array();
foreach ($rules as $rrr)
{
if (substr($rrr, 0, 9) == 'callback_')
{
$callback = TRUE;
$new_rules[] = $rrr;
}
}
if ($callback == FALSE)
{
return;
}
$rules = $new_rules;
#6

[eluser]Ricardo Machado[/eluser]
[quote author="xwero" date="1232780230"]get the form_validation file from SVN and check if the error is still there because i can't find your line in the whole file.[/quote]

Hum...

# Revision 1599: /trunk/system/libraries

The code below starts @ line: 589
Code:
// Call the function that corresponds to the rule
            if ($callback === TRUE)
            {
                if ( ! method_exists($this->CI, $rule))
                {        
                    continue;
                }
                
                // Run the function and grab the result
                $result = $this->CI->$rule($postdata, $param);

                // Re-assign the result to the master data array
                if ($_in_array == TRUE)
                {
                    $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
                }
                else
                {
                    $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
                }
            
                // If the field isn't required and we just processed a callback we'll move on...
                if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
                {
                    return;
                }
            }

(I put a little more code in order for you to find it faster....)

Hope it helps find it... Wink Hugz
#7

[eluser]Colin Williams[/eluser]
I always see this getting complained about. Seems like it would have been fixed by now.




Theme © iAndrew 2016 - Forum software by © MyBB