Welcome Guest, Not a member yet? Register   Sign In
Validation, re-populating array checkbox
#1

[eluser]Kopel[/eluser]
Hi,

I need to re-populate my checkbox when my form has been submitted with errors.
Everything is fine with regular checkbox, but my checkbox has to be an array (name="mycheck[]").

See below my simplified code as an example.

- In order to have an error i leave the input blank
- I check some boxes
- I submit the form
- The error on the input is found, the controller reload the form view
- The checkboxes are unchecked...

I tried all day long to find the solution of this apparently simple problem, but i'm still unsuccessful.
Please, let me know what is wrong with my code.
Thanks.

View file:
Code:
<?php echo form_open('sandbox'); ?>

&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username; ?&gt;" size="50" /><br />
&lt;?php echo $this->validation->error_string; ?&gt;

&lt;input type="checkbox" name="mycheck[]" value="1" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '1'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="2" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '2'); ?&gt; /><br />
&lt;input type="checkbox" name="mycheck[]" value="3" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '3'); ?&gt; /><br />

&lt;input type="submit" value="Submit" /&gt;

&lt;/form&gt;

Controller file
Code:
&lt;?php

class Sandbox extends Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
            
        $rules['username'] = "required";
        $rules['mycheck']  = "";
    
        $this->validation->set_rules($rules);
            
      
        $fields['username'] = "user name";
        $fields['mycheck']  = "my check";
    
        $this->validation->set_fields($fields);
            
      
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('sandbox');
        }
        else
        {
          echo "success";
          print_r($_POST['mycheck']);
        }
    }
}
?&gt;


Messages In This Thread
Validation, re-populating array checkbox - by El Forum - 09-25-2007, 11:02 AM
Validation, re-populating array checkbox - by El Forum - 09-25-2007, 11:30 AM
Validation, re-populating array checkbox - by El Forum - 09-25-2007, 07:23 PM
Validation, re-populating array checkbox - by El Forum - 09-25-2007, 08:01 PM
Validation, re-populating array checkbox - by El Forum - 05-17-2008, 09:33 PM
Validation, re-populating array checkbox - by El Forum - 05-17-2008, 11:28 PM
Validation, re-populating array checkbox - by El Forum - 12-29-2009, 03:18 AM
Validation, re-populating array checkbox - by El Forum - 07-16-2013, 08:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB