CodeIgniter Forums
CI 1.7 form_validation bug regarding 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: CI 1.7 form_validation bug regarding callbacks (/showthread.php?tid=12752)



CI 1.7 form_validation bug regarding callbacks - El Forum - 10-30-2008

[eluser]Bogdan Tanase[/eluser]
Hi,

I'm not 100% sure, but I think I've spotted a bug that in some cases prevents execution of more than one callback function:

In Form_validation.php we have the following snippet at line 610:

Code:
// 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'm pretty sure that instead of return we should have continue, otherwise the next rule will not execute.


CI 1.7 form_validation bug regarding callbacks - El Forum - 11-01-2008

[eluser]Bogdan Tanase[/eluser]
Can anyone confirm that this is indeed a bug?


CI 1.7 form_validation bug regarding callbacks - El Forum - 11-05-2008

[eluser]Unknown[/eluser]
Bogdan,

Yes, I think it is a bug. In my application, I need to have two call back functions in series:
* the first callback rewrites the field contents (it is verified against the database) and thus must return a string;
* the second callback looks at the rewritten field to determine if it was completely valid or not, and therefore returns a boolean. If the field is not valid, I still want the user presented with the rewritten field.

I couldn't understand the logic at Form_validation.php:610, and in the end commented out the return - an outcome very similar to using continue.

So I believe this code should be reviewed to ensure multiple callbacks can be called, as occurs with inbuilt functions.


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-03-2009

[eluser]macigniter[/eluser]
I just had the same issues with a callback function that was followed by an md5. This is my rule for the ‘example‘ field:

Code:
$this->form_validation->set_rules('example', 'Example', 'xss_clean|callback_valid_example|md5');

The callback can be very simple and just be
Code:
function valid_example()
{
   return true;
}

md5 just isn't being called!! :-(

BUT, if you add 'required' to the rules, it works and md5 is being applied to the value.

With the fix described above (replacing 'return' with 'continue') it does work without the 'required' rule. So I guess this is really a bug. Can anyone confirm?


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-03-2009

[eluser]Bogdan Tanase[/eluser]
I believe they fixed it in SVN. Download the latest version Wink


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-03-2009

[eluser]macigniter[/eluser]
Cool!! I could've checked that myself I guess ;-) But great to know that it's fixed!


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-03-2009

[eluser]skunkbad[/eluser]
This had messed me up in the past, but it really can't be considered a bug, because the docs don't say that you can use more than one callback per field when setting up your rules. The docs should say there is a limit of one callback per rule to avoid confusion, but this has been covered in the forum many times, so it wouldn't take long for somebody to learn what they are doing wrong.


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-04-2009

[eluser]macigniter[/eluser]
[quote author="skunkbad" date="1252054713"]...it really can't be considered a bug, because the docs don't say that you can use more than one callback per field when setting up your rules...[/quote]

I disagree, because multiple callback functions DO work when the rules are required. That is a weird behaviour imho.

[quote author="skunkbad" date="1252054713"]The docs should say there is a limit of one callback per rule to avoid confusion, but this has been covered in the forum many times, so it wouldn't take long for somebody to learn what they are doing wrong.[/quote]

Since the documentation is one of CodeIgniter's greatest advantages over other frameworks I think that searching the forum isn't an option especially for a newbie.

But now that it's 'fixed' we don't have to worry about it :-)


CI 1.7 form_validation bug regarding callbacks - El Forum - 09-04-2009

[eluser]skunkbad[/eluser]
[quote author="macigniter" date="1252067391"][quote author="skunkbad" date="1252054713"]...But now that it's 'fixed' we don't have to worry about it :-)[/quote]

I'm really looking forward to this next release!