Codeigniter验证复选框组

时间:2013-01-22 10:34:08

标签: php codeigniter validation html-form

我想要使用Codeigniter 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
    }
}

它提供了所需的归档消息,但即使我选择了一个或所有复选框,它仍会提供所需的消息。

1 个答案:

答案 0 :(得分:6)

查看代码,在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']; ?>" />

解决此问题并告诉我错误是什么,我会更新我的回答