Welcome Guest, Not a member yet? Register   Sign In
How to validate a Checkbox form in Codeigniter and re-populate checked

Obivously, I have 2 problems.

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.

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>
&lt;?php echo form_open('signup/preferences'); ?&gt;
&lt;input type="checkbox" name="days[]" value="monday" id="days" /&gt; Monday<br />
&lt;input type="checkbox" name="days[]" value="tuesday" id="days" /&gt; Tuesday<br />
&lt;input type="checkbox" name="days[]" value="wednesday" id="days" /&gt; Wednesday<br />
&lt;input type="checkbox" name="days[]" value="thursday" id="days" /&gt; Thursday<br />
&lt;input type="checkbox" name="days[]" value="friday" id="days" /&gt; Friday<br />
<div>How would you like us to contact you? (When package arrives / for all other issues</div>
&lt;input type="checkbox" name="contact[]" value="email" id="contact" /&gt; Email<br />
&lt;input type="checkbox" name="contact[]" value="text" id="contact" /&gt; Text<br />
&lt;input type="checkbox" name="contact[]" value="cell" id="contact" /&gt; Cell<br />
<div>&lt;input type="submit" value="Send" /&gt;&lt;/div>
&lt;?php echo form_close(); ?&gt;
&lt;?php echo validation_errors(); ?&gt;

To reset the checkbox values, try set_checkbox(). As for the validator issues you're having, I'd suggest manual validation for the cases the validator doesn't catch.

Theme © iAndrew 2016 - Forum software by © MyBB