• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set_checkbox in new form validation library trouble

OK, I've definitely spent way too much time and must be missing something obvious here.

I have a long complicated form but the part that is giving me grief is a simple piece.

My page is a long registration page with a half dozen consecutive forms. It's structured like this so I can reuse the forms in "edit mode" later on, one at a time.

I have some data in a mysql db table, from a successful registration, consisting simply of 8 integers that are 0/1 flags.

My form has 8 checkboxes that correspond.

I cannot figure out how to pre-populate the checkboxes. The User Guide explains repopulating with data that has been posted. I even tried faking by sticking my data into $_POST but still no luck.

Can anyone show how they are using a simple checkbox (this is not even an array of values like the User Guide example) that can be populated with 0 or 1 according to the design of the new Form Validation class?

This same form, with 8 checkboxes, should default to all 8 checked when used in "add mode" (ie. during registration).

I can't do basic old-school logic like: if ($var = 0) echo ' selected="selected"'; ... because that would be incompatible with how I am using set_checkbox() sucessfully in "add mode" (the first time the form is hit).

I hope I explained this right, as it seems like it must be a simple and common thing and I'm missing something here.


I don't use 1.7.0, but I don't think the code has changed much. In the documentation is this that describes how to use the form_checkbox:
$data = array(
    'name'        => 'newsletter',
    'id'          => 'newsletter',
    'value'       => 'accept',
    'checked'     => TRUE,
    'style'       => 'margin:10px',

echo form_checkbox($data);

// Would produce:

<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />

The definition of the form_checkbox function is
form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
Hope this helps.


Thanks for your reply. It sounds like you are using the Form Helper.

I'm looking at the new Form Validation class, which replaces the deprecated Validation class.

I think I got something working now, needs a bit more testing, but it feels kludgy. If anybody else is reading this thread and is using the new Form Validation class, I found that if I want to manually set a checkbox with a value (not a value from a previously submitted form, but one I already have, in my database for example), I had to explicitly modify my form validation object instance as in the following:

$this->form_validation->_field_data['myfieldname'] = 'myvalue';

So, one of two things:

A. I'm missing the proper way to do this.
B. This validation class still needs some methods to explicitly set such values.

I don't feel good about modifying "supposedly private" variables, so if someone can point me towards a cleaner solution, I'd be grateful.

Maybe the thinking was that this falls outside the scope of validation. The problem is, I want to use the same form for adding and editing (this is common). I could not find a way to use set_checkbox() when I already had a value in mind (as in "edit" mode).


[eluser]Future Webs[/eluser]
did you make any progress on this im still a bit baffled

i need to

be able to set a default on an insert form
be able to show the value from the db on an edit form
be able to show the chosen value if validation fails

does anyone have an example of a working checkbox & radio button setup using 1.70 ?


I got things to work eventually, but I find it hard to believe this is really the best way. If there's a better way, someone please post it.

Simplified for the sake of example. This excerpted code is for a bunch of checkbox preferences as to which messages a user chooses to receive on the website.

$this->_validateMessages();  // validation rules for this part of the form
$acceptFriendRequestgenmember = $this->MemberModel->getMessageSettings($this->userType,

// This is the ugly way I found to force the code to act as if the DB value was submitted in a previous form
$this->form_validation->_field_data['acceptFriendRequestgenmember'] = $this->acceptFriendRequestgenmember;

public function getMessageSettings($userType, $memberTypeID) {
        $sql = 'SELECT acceptFriendRequestgenmember
                FROM ' . $userType . '
                WHERE ' . $userType . 'ID=' . $memberTypeID;
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row->acceptFriendRequestgenmember);

<input type="checkbox" name="acceptFriendRequestgenmember" value="1" <?php echo set_checkbox('acceptFriendRequestgenmember', '1', TRUE)?> />

Note that I'm using the value "1" for a checked checkbox, and "0" for an unchecked checkbox. The View code checks to see if what was submitted (or faked as being submitted) is equal to 1. If nothing was submitted (or faked), ie. this is an insert, the default of "TRUE" (which causes it to have 'checked="checked"') is supplied to the set_checkbox() function.

Hope that helps. I realize it's kind of tricky, trickier than should be necessary.

And again, I'm not trying to be too critical of Code Igniter because Code Igniter has saved me a lot of time and helped enforce some good coding habits, but I think the new Form Validation class still needs a little work.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.