CodeIgniter Forums
Extending Validation class to call a method that returns a modified value - 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: Extending Validation class to call a method that returns a modified value (/showthread.php?tid=28571)



Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]housecor[/eluser]
I understand how to extend CI's validation class to add additional methods that return bool, but how do I create method that modifies the value being validated?

For example, you can call native PHP functions in your validation like this:

"required|strtoupper"

I'd like to extend the validation class so I can call blank_to_null as part of my validation so any fields posted blank are converted to null before db insertion. Anyone have an example of how to pull that off?


Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]coolgeek[/eluser]
Quote:If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.

Form Validation: Codeigniter User Guide


Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]Eric Cope[/eluser]
I extend the form_validation class instead of callbacks so I can use the functions on more than one controller. It would be nice if these functions had the same features. Is that possible?


Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]danmontgomery[/eluser]
Have you tried it?


Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]Eric Cope[/eluser]
[quote author="noctrum" date="1268706200"]Have you tried it?[/quote]

I assume you are talking about my comment. I have tried it, but it does not work. I could be mistaken though, since my testing environment included no-coffee-late-night-programming...


Extending Validation class to call a method that returns a modified value - El Forum - 03-15-2010

[eluser]WanWizard[/eluser]
Odd.

Looking at the code in the _execute method, it does check for non-bool return values, on callbacks, internal PHP functions, and form_validation methods...

Code:
$this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;