如何在codeigniter中验证数组值

时间:2013-07-21 11:45:54

标签: codeigniter validation

我有自己的工作经验,可以通过输入动态添加组织名称,部门名称,职位和持续时间。

<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>

在CI中进行验证时,我执行了以下操作:

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

并显示错误:

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

现在的问题是它不会将持续时间验证为整数。即使我放了一些随机的字母字符,它也不会显示错误。

当我验证如下:

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

它不允许我提交表格,因为它需要持续时间作为强制性而不是。

我该如何解决?

欢迎任何帮助/建议。感谢。

2 个答案:

答案 0 :(得分:8)

正如@Hashem Qolami所建议的那样,我在代码中做了以下更改,并且工作正常。

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

并显示我的错误如下

 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."]");
  }

答案 1 :(得分:3)

It doesn't let me submit the form如果是这样,这听起来像个错误。

暂时,您可以检查duration[]数组是否为empty

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

如果找到主要解决方案,我会更新我的答案。


<强>更新 这是我预期的错误,我在CodeIgniter Repository opened a PR,这将解决这个问题。