9

私は、組織名、部署名、役職、期間などの入力で動的に追加される形式の実務経験を持っています。

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

必須ではない期間がかかるため、フォームを送信できません。

どうすれば解決できますか?

どんな助け/提案も大歓迎です。ありがとう。

4

2 に答える 2

10

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 リポジトリでPR を開き、この問題を修正しました。

于 2013-07-21T14:49:38.930 に答える
9

@ 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."]");
  }
于 2013-07-23T05:32:31.953 に答える