2013-01-22 188 views
0

我有我想用笨Form_Validation類驗證此HTML代碼:笨驗證複選框組

<form id="thread" method="POST"> 
    <input type="checkbox" name="checkAllAuto" id="checkAll" onclick="CheckAll(this.id)" /><?php echo __t($dir_lang,'request_select_all');?> 
    <br /> 
    <?php foreach ($threads as $thread): ?> 
    <input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" /> <?php echo $thread['th_title']; ?> <br> 
    <?php endforeach; ?> 
    <br /> 
    <input type="checkbox" name="mom" /> <?php echo __t($dir_lang,'request_send');?> 
    <br /> 
    <select name="cat"> 
     <?php foreach ($cats as $cat): ?> 
     <option value="<?php echo $cat['id']; ?>"> <?php echo $cat['title']; ?> </option> 
     <?php endforeach; ?> 
    </select> 
    <br /><input type="submit" value="<?php echo __t($dir_lang,'submit');?> " /><br> 
</form> 

我試圖驗證複選框的拳頭組通過使用Form_Validation

if ($this->input->isMethodPost()) 
{ 
    $this->tplData['showMess'] = true; 
    $this->load->library('form_validation'); 
    $this->lang->load('form_validation', 'english'); 
    $this->form_validation->set_error_delimiters('<h3>', '</h3>'); 
    $this->form_validation->set_rules('options[]','options', 'required'); 
    if ($this->form_validation->run() == FALSE) 
    { 
     $this->tplData['showError'] = true; 
     $this->tplData['mess'] = array(validation_errors()); 
    }else{ 
     // my work 
    } 
} 

它提供了所需的歸檔消息,但即使我選擇了一個或所有複選框,仍然會提供所需的消息。在你的代碼

+0

有沒有人可以幫助我? –

+0

@SebastianWolff –

回答

3

尋找你設置options[]您的姓名在set_rules

$this->form_validation->set_rules('options[]','options', 'required'); 

,而在你的HTML的複選框的名字是option[]

<input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" /> 

修復這一點,並告訴我的錯誤是什麼,我會更新我的回答

+0

對我來說,它只適用於我使用以下內容: $ this-> form_validation-> set_rules('option []','option []','required'); 」/> – Hana90

+0

這是用於CodeIgniter 3.1.2 – Hana90