如何在codeigniter中验证数组值 [英] 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>
在 CI 中进行验证时,我执行了以下操作:
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");
}
并显示错误为:
<?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.
当我验证如下:
$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.
我该如何解决?
欢迎任何帮助/建议.谢谢.
any help/suggestions are welcome. thanks.
推荐答案
它不允许我提交表单
如果是这样,这听起来像是一个错误.
It doesn't let me submit the form
If so, It sounds like a bug.
暂时可以检查duration[]
数组是否为empty
:
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.
更新:正如我所料,这是一个错误,我在 CodeIgniter 存储库打开了一个 PR,这将修复这个问题.
Update: This is a bug as I expected, I opened a PR at CodeIgniter Repository, that would fix this issue.
这篇关于如何在codeigniter中验证数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!