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?


Messages In This Thread
Array Field Validation with Callback Function - by El Forum - 10-09-2012, 09:00 AM
Array Field Validation with Callback Function - by El Forum - 10-09-2012, 10:51 PM
Array Field Validation with Callback Function - by El Forum - 10-09-2012, 11:12 PM
Array Field Validation with Callback Function - by El Forum - 10-09-2012, 11:32 PM
Array Field Validation with Callback Function - by El Forum - 10-10-2012, 01:41 AM
Array Field Validation with Callback Function - by El Forum - 10-11-2012, 12:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB