[solved] validation class - callback functions |
[eluser]obiron2[/eluser]
Hi guys, I am trying to extend my knowlege of some of the more advanced functions in CI. I have a form which requires a start and end date, populated as 3 fields per date Sday,Smth,Syr and Eday,Emth,Eyr I would like to use the validation class to do the following: Validate that each field is populated and is numeric and no more than 2 digits long - easy - 'required|trim|numeric|max_length[2]'. Validate that when Sday,Smth,Syr are combined together they form a valid date Validate that the start date is before the end date Validate that the start date is after a date pulled from the database I don't really understand what is happening with $rules[] and $fields[] and what the validation class then does with them Can anyone point me in the right direction on how to combine fields together in validation rules. Thanks obiron
[eluser]ELRafael[/eluser]
http://ellislab.com/codeigniter/user-gui...ation.html Read the Callbacks: Your own Validation Functions section. :-P
[eluser]obiron2[/eluser]
I did. The examples given allow you to validate a single field in the callback. I need to validate a combination of three / six fields in a single callback. I am not clear how to define the $rules and $fields. If someome can show me how to do that and format the function call, I can write the function. The function I am looking to write is a basic date validator - do the three fields represent a valid date, is one date before another. I will post the functions back to the community. Obiron
[eluser]Michael Ekoka[/eluser]
Lets say we want to validate a date. It's typically made using 3 selectbox fields in a form (day, month, year). We don't want to validate the fields separately so we assign one callback validation method to one of the fields, as well as the name of the compound field (Date of Birth). Now, the validation doesn't care what your callback method does inside. It's only concerned with the value that it returns. So, if it returns false, the validator knows to display the error message. If it returns nothing or true the validator assumes the field is valid. Code: function my_action(){
[eluser]obiron2[/eluser]
Superstar! and I don't even need to write the function. Thankyou so much. If I need to check two dates, I suppose I either need two callback functions with different $this->input-post('XXX') values which then get passed on to the valid_date function or pass a variable through to the function and put a case statemement into the function to set the $this->input->post('xxx') If I need to do multiple validations, would I do this by getting the callback function to run multiple functions or can I do multiple callbacks. Obiron
[eluser]Michael Ekoka[/eluser]
For multiple dates, you will need a couple of steps, but this works very well if you understand what's going on. In your form declare your date values like this: Code: <html> now you would change the code above like: Code: function my_action(){
[eluser]iive[/eluser]
CodeIgniter, you are so helpful. I just came across to this problem and it is solved after I read this thread. Thanks.
[eluser]souto[/eluser]
The same goes to me. This is an important feature, since validation often requires interaction between fields, and not fields per se. Thanks a lot. |
Welcome Guest, Not a member yet? Register Sign In |