CodeIgniter Forums

Full Version: preparing vs validating
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

[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.

El Forum

[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.

El Forum

[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.