Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

validation - how to validate array value in codeigniter

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...