Array Field Validation with Callback Function |
[eluser]foxybagga[/eluser]
Hello folks, I am really enjoying working on CI - its just superb! I just wanted a tip if someone could help with the Form Validation that I need to do. My form fields looks like below Code: <label for="shift_from[1]">Shift Start</label> The view code is as below. Code: <div class="<?php if (form_error('shift_from[' . $counter . ']') != "") echo "error"; ?>"> I am having multiple shifts selectors with Shift Start and Shift End select boxes which are populating and working properly. I am settings the validation rules as below Code: $fromArray = $this->input->post('shift_from'); Code: public function check_shifts(){ The problem is the when any one or two of the shift timings are same - every select box shows the error (including the ones that are correct too) and not those individual ones. Here is a preview of how the submitted form looks like - http://i.imgur.com/FUzeY.png If you can see - the first shift has different timings - still its showing an error. The values are different and are simply strtotime. Any clues how to do it?
[eluser]Aken[/eluser]
Your callback function is checking every shift from/to combo on every call. So it will return false for every single pair if only one of them has an error. You need to have your callback only fire for that particular pair. You can use something like this: Code: // Callback rules in your $fromArray loop
[eluser]foxybagga[/eluser]
Thanks a lot Aken. This solves the issue but is resetting the select boxes whose values are correct. I am using the below code to set the value Code: set_select("shift_from[" . $counter . "]", VALUE, (FROM_DATABASE == VALUE) ? TRUE : FALSE); Kindly help - I am also working on it and in the mean while would try to answer some other questions here in the forum!
[eluser]Aken[/eluser]
I'm not sure how my suggestion would affect the set_select() method, since it should not modify the POSTed value at all (verify that your callback is ALWAYS returning true or false, otherwise it will modify the value rather than validating it). Debug it some more, and post more of your actual code if you're still having problems.
[eluser]foxybagga[/eluser]
Thanks Aken, I shall debug it more and get back in-case there are errors. Cheers! |
Welcome Guest, Not a member yet? Register Sign In |