【问题标题】:how to validate array value in codeigniter如何在codeigniter中验证数组值
【发布时间】:2013-07-20 06:25:06
【问题描述】:

我的表单工作经验是动态添加的,输入为组织名称、部门名称、职位和持续时间。

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

它不允许我提交表单,因为它需要持续时间,但它不是。

我该如何解决?

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

【问题讨论】:

  • 也许你不应该在duration 验证中添加trim(和xss_clean)?
  • @sammy 我已经更新了我的答案,请检查链接是否已添加。欢迎任何反馈。

标签: codeigniter validation


【解决方案1】:

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 存储库opened a PR,可以解决这个问题。

【讨论】:

【解决方案2】:

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

【讨论】:

猜你喜欢
  • 2017-12-19
  • 2015-09-25
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2011-07-10
  • 2012-01-02
相关资源
最近更新 更多