2016-12-09 48 views
0

我有一個表格是這樣的:如何顯示數組元素的驗證錯誤信息?

<form action="" method="post"> 
    <input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/> 
    <?php 
    for ($i=1; $i<=$ndetails; $i++) 
    { 
    ?> 
    <textarea name="mydetails[]"></textarea> 
    <?php echo form_error('mydetails[]'); ?> 
    <?php 
    } 
    ?> 
</form> 

,並在控制器中,我使用了一個表單驗證是這樣的:

for ($i=1; $i<=$this->input->post('ndetails'); $i++) 
{ 
    $this->form_validation->set_rules('mydetails[]', 'Day '.$i, 'trim|required'); 
} 

的問題是,當有超過1個textarea的,該form_error('mydetails[]')只能說明最後的錯誤信息。如何在每個textarea之後單獨顯示錯誤?

+0

看着http://stackoverflow.com/a/17802145/689579,它看起來像你可以設置數組鍵 - >']「> ...','<?php echo form_error('mydetails ['。$ i。']'); $'','trim_ required');'$ this,> form_validation-> set_rules('mydetails ['。$ i'''','Day' – Sean

回答

0

使用此:代碼沒有經過測試

<form action="" method="post"> 
    <input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/> 
    <?php 
    for ($i=1; $i<=$ndetails; $i++) 
    { 
    $j=$i-1; 
    echo "<textarea name='mydetails[".$j."]'></textarea>"; 
    echo form_error('mydetails['.$j.']'); 
    } 
    ?> 
</form>