How to validate a Checkbox form in Codeigniter and re-populate checked

18,825

Solution 1

Firstly; validate your checkbox using a custom callback, you can do whatever you want to validate the contents.

Secondly; use an if to set the checked="checked".

$my_option_values is a hash array of the checkbox options.

<input type="checkbox" name="my_option[]" value="blah" 
<?php ( in_array('blah', $_POST['my_option']) || in_array('blah', $my_option_values) ) ? print 'checked="checked"' : FALSE; ?> />

If you want, make it a function and put it in a helper file.

Solution 2

The issue is that browsers, by specification, aren't supposed to send a value for unchecked checkboxes. Checkboxes are, by definition, optional fields. What should the value be if it's unchecked? Null? 0? HTML doesn't have a built-in handler for this use of checkboxes.

But, there are some workarounds. I like to use javascript to enable a hidden field with the "unchecked" value I want. For example (with jQuery):

<input type="checkbox" name="foo" value="1" />
<script type="text/javascript">
$(function() {
    $('input[name="foo"]').change(function() {
        var $t = $(this);
        if ( !$t.attr('checked'))
        {
            $t.after('<input type="hidden" name="foo" value="0" />');
        }
        else
        {
            $t.next().remove();
        }
    });
});
</script>

This isn't as much a CI issue as it is an "html form interface" issue.

Share:
18,825
SNaRe
Author by

SNaRe

Updated on June 04, 2022

Comments

  • SNaRe
    SNaRe almost 2 years

    Obivously, I have 2 problems.

    1. When I submit a checkbox form which is an array value and non-cheched form, It doesn't show me validation error such as 'this field is required'. It shows error when I check one of them. Normally it should have shown me even though I don't check anything on the form and just press submit.

    2. How can I repopulate a checkbox as checked when I submit as checked. Think like set_value. In set_value it populates previous data. At this I want it to be auto checked when if I had submitted.

    My controller

    function preferences() {
        $user = $this->ion_auth->user()->row();
        $data['first_name'] = $user->first_name;
        $data['last_name'] = $user->last_name;
        $data['user_id'] = $user->id;
        $data['address'] = $this->main_model->office_zip_match($user->zipcode);
    
        $this->form_validation->set_rules('days[]', 'Days', 'required');
        $this->form_validation->set_rules('contact[]', 'Contact', 'required');
    
        if ($this->form_validation->run() == true) {
            echo print_r($this->input->post());
        } else {
            $this->load->view('auth/preferences', $data);
        }
    }
    

    My view:

    <div>Days to Deliver</div>
    <?php echo form_open('signup/preferences'); ?>
    <input type="checkbox" name="days[]" value="monday" id="days" /> Monday<br />
    <input type="checkbox" name="days[]" value="tuesday" id="days" /> Tuesday<br />
    <input type="checkbox" name="days[]" value="wednesday" id="days" /> Wednesday<br />
    <input type="checkbox" name="days[]" value="thursday" id="days" /> Thursday<br />
    <input type="checkbox" name="days[]" value="friday" id="days" /> Friday<br />
    <div>How would you like us to contact you? (When package arrives / for all other issues</div>
    <input type="checkbox" name="contact[]" value="email" id="contact" /> Email<br />
    <input type="checkbox" name="contact[]" value="text" id="contact" /> Text<br />
    <input type="checkbox" name="contact[]" value="cell" id="contact" /> Cell<br />
    <div><input type="submit" value="Send" /></div>
    <?php echo form_close(); ?>
    <?php echo validation_errors(); ?>
    
  • SNaRe
    SNaRe over 12 years
    I solved the problem in the same way but I have never had a problem with native PHP and html forms. I have had only with Codeigniter.