2013-07-21 22 views
7

私は自分のフォームに、組織名、部門名、位置、期間として入力された作業経験があります。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> 

私は以下のなかった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"); 

それはそれがされていないとして必須の期間がかかるとして私がフォームを送信することはできません。

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

助けを歓迎します。ありがとう。

+0

をshouldn次のように私の誤りを表示; tは 'duration'検証に(あまりと' xss_clean') 'trim'を追加しますか? –

+0

@サミー私は私の答えを更新しました。リンクが追加されていることを確認してください。いかなるフィードバックも歓迎されます。 –

答えて

3

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

私はメインの解決策を見つけた場合、私は私の答えを更新します。


更新: これは、この問題を解決するだろう、私は予想通りのバグ、I CodeIgniterのリポジトリでopened a PR、です。

+0

すみません...この質問を一見していただけますか? [http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation](http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation) – Shihas

6

@ 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."]"); 
    } 
+0

すみません...あなたはこの質問を見てください。 [http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation](http://stackoverflow.com/questions/39941976/codeigniter-array-backend-validation) – Shihas

+0

すばらしい答えは、本当に助けます.. –

関連する問題