10

I have in my form work experience which is added dynamically with input as organisation name, department name, position 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>

While validating 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 displayed error as:

<?php
    echo form_error("organization[]");
    echo form_error("department[]"); 
    echo form_error("positions[]"); 
    echo form_error("duration[]");
?> 

Now the problem is it doesn't validate the duration as integer. Even if I put some random alpha characters it doesn't show errors.

When I validate as follows:

$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");

It doesn't let me submit the form as it takes duration as mandatory which it isn't.

How do I solve it?

any help/suggestions are welcome. thanks.

2
  • Maybe you shouldn;t add trim (and xss_clean too) to duration validation? Commented Jul 21, 2013 at 11:53
  • @sammy I've update my answer, check the link has been added, please. any feedback is welcomed. Commented Jul 21, 2013 at 19:02

2 Answers 2

13

It doesn't let me submit the form If so, It sounds like a bug.

Temporarily, you can check whether duration[] array is empty or not:

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

I'll update my answer if I found the main solution.


Update: This is a bug as I expected, I opened a PR at CodeIgniter Repository, that would fix this issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Excuse me... Can you please have a look on this question. http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation
11

So as suggested by @ Hashem Qolami I did the following changes in 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."]");
  }

2 Comments

Excuse me... Can you please have a look on this question. http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation
how do you set data for the form_validation in this case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.