How to check array value in codeigniter

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.

+7
validation codeigniter
source share
2 answers

It doesn't let me submit the form If so, that sounds like an error.

You can temporarily check if the duration[] array is empty or not:

 if (! empty($_POST['duration'])) { $this->form_validation->set_rules('duration[]',"Duration", "trim|xss_clean|numeric"); } 

I will update my answer if I find a basic solution.


Update: This is a bug, as I expected, I opened PR in the CodeIgniter repository, which will fix this problem.

+3
source share

So, as @Hashem Qolami suggested, I made the following changes to my code, and it worked.

 $organization = $this->input->post('organization'); $department = $this->input->post('department'); $positions = $this->input->post('positions'); $duration = $this->input->post('duration'); foreach($organization as $ind=>$val) { $org = $organization[$ind]; $dept = $department[$ind]; $pos = $positions[$ind]; $dura = $duration[$ind]; $this->form_validation->set_rules("organization[".$ind."]", "Organization", "trim|xss_clean|max_length[1]"); $this->form_validation->set_rules("department[".$ind."]", "Department", "trim|xss_clean|max_length[50]"); $this->form_validation->set_rules("positions[".$ind."]", "Position", "trim|xss_clean|max_length[40]"); if(!empty($dura)) $this->form_validation->set_rules("duration[".$ind."]", "Duration", "trim|xss_clean|integer"); } 

and displayed my errors as follows

  for($i = 0; $i < count($organization); $i++) { echo form_error("organization[".$i."]"); echo form_error("department[".$i."]"); echo form_error("positions[".$i."]"); echo form_error("duration[".$i."]"); } 
+6
source share

All Articles