Welcome Guest, Not a member yet? Register   Sign In
Array Field Validation with Callback Function
#1

[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>
<select name="shift_from[1]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>                                

<label for="shift_to[1]">Shift End</label>
<select name="shift_to[1]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>                                

<label for="shift_from[2]">Shift Start</label>
<select name="shift_from[2]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>                                

<label for="shift_to[2]">Shift End</label>
<select name="shift_to[2]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>

The view code is as below.

Code:
<div class="&lt;?php if (form_error('shift_from[' . $counter . ']') != "") echo "error"; ?&gt;">
<label for="shift_from[&lt;?php echo $counter; ?&gt;]">Shift Start</label>
    <select name="shift_from[&lt;?php echo $counter; ?&gt;]" class="span2">
    </select>                                
&lt;?php echo form_error('shift_from[' . $counter . ']'); ?&gt;

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');                
for($i=1;$i<=count($fromArray);$i++){
    $this->form_validation->set_rules('shift_from['.$i.']', 'Shift Start', 'callback_check_shifts');
    $this->form_validation->set_rules('shift_to['.$i.']', 'Shift Start', 'callback_check_shifts');
}

Code:
public function check_shifts(){
$fullArray = $this->input->post();        
for($i=1;$i<=count($fullArray['shift_from']);$i++){
    if($fullArray['shift_from'][$i] == $fullArray['shift_to'][$i]){                
$this->form_validation->set_message('check_shifts', "Shift Start and End times cannot be the same.");
return FALSE;
    }  
}

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?
#2

[eluser]foxybagga[/eluser]
anything folks?
#3

[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
$this->form_validation->set_rules('shift_from['.$i.']', 'Shift Start', 'callback_check_shifts['.$i.']');
$this->form_validation->set_rules('shift_to['.$i.']', 'Shift Start', 'callback_check_shifts['.$i.']');

public function check_shifts($input, $shift_set)
{
    // Do your check here, using $shift_set as the key number, replacing $i
}
#4

[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!
#5

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

[eluser]foxybagga[/eluser]
Thanks Aken, I shall debug it more and get back in-case there are errors. Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB