Form validation checkbox in ci not working

I have a form with a checkbox and an input field but validate if the checkboxes are not processed and show validation error.

Mail Controller:

$this->load->helper('form');
$this->load->library('form_validation');
$rules=array(
    array(
        'field'=>'title',
        'label'=>'Title',
        'rules'=>'required'),
    array('field'=>'content',
        'label'=>'Content',
        'rules'=>'required'),
    array('field'=>'category',
        'label'=>'category',
        'rules'=>'required'));


$this->form_validation->set_rules($rules);

      

Index type:

<div class="box-content">
    <h3>category</h3>
    <?php 
        $pcat=isset($post_cat)?$post_cat:array();
        foreach($category as $key => $value){
            $checked=in_array($value['category_id'],$pcat);
            $name='category['.$value['name'].']';
            echo '<label style="display:block;">';
            echo $value['name'];
            echo form_checkbox(
            array(
            'name'=>$name,
            'value'=>$value['category_id'],
            'checked'=>$checked));}
            echo '</label>';
    ?>
    <?php  echo form_error('category');?>
</div>

      

please help me decide what

+3


source to share


1 answer


Since your category entry is an array. You need to check using the array. Change this in your controller:



 array('field'=>'category[]',
            'label'=>'category',
            'rules'=>'required'));

      

0


source







All Articles