Welcome Guest, Not a member yet? Register   Sign In
How to repopulate check box group?

I've searched around, but I couldn't find a good answer to repopulating check boxes. I have some validation rules on text fields, but the user also has the option to check some check boxes (There are no validation rules on the check boxes).

If the user fills in the text fields erroneously I would like the state of the check boxes to be kept, so they don't have to refill those in when validation on the text fields fails. I'm using a check box group, so my checkbox name is something like checkbox_name[]. I've tried using $this->validation->set_checkbox('checkbox_name[]', 1), but it doesn't seem to do anything.

[eluser]Michael Wales[/eluser]
View the set_checkbox() section at the bottom of the Validation class.

Why is your name an array? Why not make it just a standard var?

I think he uses an array so that he can get multiple checkboxes selected inside one var.

i was using slightly different method to repopulate checkboxes:

example, inside view:
field names are changed, this is only an example.
                <SELECT NAME="something">
                $sql = "SELECT * from table order by field";
                $query = $this->db->query($sql);
                foreach ($query->result() as $row)
                    if ($row->field1 == $this->validation->field1)
                        print "<OPTION VALUE=\"".$row->field1."\" selected>".$row->field2."\n";
                        print "<OPTION VALUE=\"".$row->field1."\">".$row->field2."\n";

This way i am operating with field values, not checkboxes, and i can easily setup validation to work with values, just like it would work with input fields.
It is easier for me that way, i am not sure if it breaks MVC philosophy...

Unfortunately I need to use an array of check boxes because of getting names and checking the group as a whole. Plus the number of check boxes is not known until a query runs. That looks like a really good solution smith, I'm going to give that a try, thanks Smile

[eluser]Michael Wales[/eluser]
heh - sorry about that. I was trashed last night and I am coming back through the forums this morning reading my replies wondering how I didn't grasp what people were asking. Looks like every single one of my replies last night didn't help at all... Smile

No worries Smile I finally got it to work, but it wasn't with the validation class method set_check(). I had to loop through POST array and check to see if the value from the query matches up with the one in the POST. It's not the prettiest solution but it works.

Theme © iAndrew 2016 - Forum software by © MyBB