私は、組織名、部署名、役職、期間などの入力で動的に追加される形式の実務経験を持っています。
<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");
必須ではない期間がかかるため、フォームを送信できません。
どうすれば解決できますか?
どんな助け/提案も大歓迎です。ありがとう。