2

I have this html code that I want to validate using the Codeigniter Form_Validation class :

<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>

I'm trying to validate the fist group of the check box by using 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
    }
}

It gives required filed message but even if I choose one or all the checkbox it still gives the required message.

1
  • is there any one can help me ?? Commented Jan 29, 2013 at 10:35

1 Answer 1

7

Looking at your code you set options[] as your name in set_rules:

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

while on your HTML your checkbox name is option[]

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

Fix this and tell me what the error is and i'll update my answer

Sign up to request clarification or add additional context in comments.

2 Comments

for me it only worked if I use the following: $this->form_validation->set_rules('option[]','option[]', 'required'); <input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" />
This is for CodeIgniter 3.1.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.