[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'); ?>
<input type="text" name="username" value="<?php echo $this->validation->username; ?>" size="50" /><br />
<?php echo $this->validation->error_string; ?>
<input type="checkbox" name="mycheck[]" value="1" <?php echo $this->validation->set_checkbox('mycheck', '1'); ?> /><br />
<input type="checkbox" name="mycheck[]" value="2" <?php echo $this->validation->set_checkbox('mycheck', '2'); ?> /><br />
<input type="checkbox" name="mycheck[]" value="3" <?php echo $this->validation->set_checkbox('mycheck', '3'); ?> /><br />
<input type="submit" value="Submit" />
</form>
Controller file
Code:
<?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']);
}
}
}
?>