• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
preparing vs validating

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?

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

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

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.