sam*_*shi 9 validation codeigniter
我有自己的工作经验,可以通过输入动态添加组织名称,部门名称,职位和持续时间.
<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>
Run Code Online (Sandbox Code Playgroud)
在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");
}
Run Code Online (Sandbox Code Playgroud)
并显示错误:
<?php
echo form_error("organization[]");
echo form_error("department[]");
echo form_error("positions[]");
echo form_error("duration[]");
?>
Run Code Online (Sandbox Code Playgroud)
现在的问题是它不会将持续时间验证为整数.即使我放了一些随机的字母字符,它也不会显示错误.
当我验证如下:
$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");
Run Code Online (Sandbox Code Playgroud)
它不允许我提交表格,因为它需要持续时间作为强制性而不是.
我该如何解决?
欢迎任何帮助/建议.谢谢.
因此,正如@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");
}
Run Code Online (Sandbox Code Playgroud)
并显示我的错误如下
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."]");
}
Run Code Online (Sandbox Code Playgroud)
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");
}
Run Code Online (Sandbox Code Playgroud)
如果找到主要解决方案,我将更新答案。
更新: 这是我所期望的错误,我在CodeIgniter存储库中打开了PR,可以解决此问题。
| 归档时间: |
|
| 查看次数: |
23330 次 |
| 最近记录: |