I have experience with my form, which is dynamically added with the login name, organization name, location and duration.
<tr> <td><span class="serial_no">1</span></td> <td><input type="text" name="organization[]" size="50"/></td> <td><input type="text" name="department[]" size="50"></td> <td><input type="text" name="positions[]" size="40"></td> <td><input type="text" name="duration[]"></td> </tr>
When checking in CI, I did the following:
$organization = $this->input->post('organization'); $department = $this->input->post('department'); $positions = $this->input->post('positions'); $duration = $this->input->post('duration'); //printr($organization); for($i = 0; $i < count($organization); $i++) { $org = $organization[$i]; $dept = $department[$i]; $pos = $positions[$i]; $dura = $duration[$i]; $this->form_validation->set_rules($org, "Organization", "trim|xss_clean|max_length[1]"); $this->form_validation->set_rules($dept, "Department", "trim|xss_clean|max_length[50]"); $this->form_validation->set_rules($pos, "Position", "trim|xss_clean|max_length[40]"); $this->form_validation->set_rules($dura, "Duration", "trim|xss_clean|integer"); }
And the error is displayed as:
<?php echo form_error("organization[]"); echo form_error("department[]"); echo form_error("positions[]"); echo form_error("duration[]"); ?>
Now the problem is that it does not check the duration as a whole. Even if I put some random alpha characters, it shows no errors.
When I check the following:
$this->form_validation->set_rules('organization[]', "Organization", "trim|xss_clean|max_length[50]"); $this->form_validation->set_rules('department[]', "Department", "trim|xss_clean|max_length[50]"); $this->form_validation->set_rules('positions[]', "Position", "trim|xss_clean|max_length[40]"); $this->form_validation->set_rules('duration[]',"Duration", "trim|xss_clean|numeric");
This does not allow me to present the form, because it requires a duration as mandatory, which it is not.
How to solve it?
Any help / suggestions are welcome. thanks.