CodeIgniter Forums
preparing vs validating - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: preparing vs validating (/thread-10761.html)



preparing vs validating - El Forum - 08-11-2008

[eluser]plainas[/eluser]
The validation class allows one to validate data using a customised callback function.
All the callback examples in the docs return either true or false.
It's also said that any native php function that accepts one argument can be used.

My question is:
Is it possible to use the callback trick to prepare data, or can it only be used for validation purposes?
Taking it a bit further, is it possible to use the same function to prepare and validate?


preparing vs validating - El Forum - 08-11-2008

[eluser]Colin Williams[/eluser]
The user guide even has an example, using the trim function. I don't think your custom callbacks can alter the $this->validation->{field_name} properties, but give it a shot.


preparing vs validating - El Forum - 08-11-2008

[eluser]Eric Cope[/eluser]
The answer to your question is yes. However, it is tricky and easier to do by extending the validation class instead of using callbacks. I wrote a function to actively edit $this->validation->field_name, but I don't have that code on this computer. I can post it later if you like.


preparing vs validating - El Forum - 08-12-2008

[eluser]plainas[/eluser]
I tried and it works perfectly. One function can validate and return false if whatever validation rules are not matched, and return the prepared data otherwise.

I love this validation class. Pipping validation and preparing functions in such fashion is f... elegant. And the callback thing is damn practical.
This framework rocks.

Thank you for the quick replies and for the kick ass piece of software that CI is.