Welcome Guest, Not a member yet? Register   Sign In
Checkboxes, checked by default, how to ?

[eluser]Référencement Google[/eluser]

I have a very simple problem on how to handle that simply with CI. I would like some checkboxes checked attribute to be checked by default (when loading the page the first time)

If I make that:
<input class="checkbox" id="alow_pub" name="alow_pub" type="checkbox" value="1"<?=$this->validation->set_checkbox('alow_pub', '1')?> />
There is no options that the checkbox have to be already checked before posting.

How to do it ?


You could try combining form_checkbox() from the Form Helper with the code you are using on the validation helper.

According to the user guide, extra data can be passed, so perhaps something like this would work (Note: I haven't tested it):

$validation = $this->validation->set_checkbox('alow_pub', '1')

echo form_checkbox('alow_pub', '1', TRUE, $validation)

[eluser]Référencement Google[/eluser]
thanks for your answer Jgsd, unfortunally it doesn't work as expected.
With your exemple, the check box is set by default to checked, this is ok, but once submited, it doesn't keep the state and always come back to checked.

Anyone have solution ?

The problem is that $this->validation->run() returns FALSE both if the validation didn't succeed and if the form is loaded for the first time. This is obviously the correct behavior for this call.

So, what we need is a way to distinguish whether or not the form has been submitted yet. If it has, we can have 'checked="checked"' appended by default but, if it has, we can let it use the value from $this->validation->set_checkbox('alow_pub', '1');

Trouble is, I'm not sure how to do this. :down: We can't check for $this->input->post('alow_pub') because, again, it has the same state both when the checkbox hasn't been checked and when the form hasn't been submitted.

If you figure this out by yourself, please post here as I would like to know, too!

[eluser]Colin Williams[/eluser]
$this->validation->run() should definitely have 3 possible results for this very situation.

[eluser]Glen Swinfield[/eluser]
As a quick fix you could use:

// is a post - do something
// is not posted make checkbox checked

[eluser]Colin Williams[/eluser]
Well, that's obviously the correct code, Pat (Codepat?), but it'd be nice to have this work within the validation class. I'm not lost on how to do this, it's just an idea someone might like to consider.

[eluser]Référencement Google[/eluser]
Thank you for your answers, it is working as you said Codepat.

I also posted here the feature request because I really think like you Colin, that the validation class is too much incomplete:


You could extend the form helper with an additional function as follows:

* Conditional Checkbox Field
* @access public
* @param mixed
* @param string
* @return string

function form_checkbox_conditional($data = '', $value = '', $extra = '')
$defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

if (is_array($data) AND array_key_exists('compareVal', $data)){
  $checked = ($data['value'] != $data['compareVal']) ? FALSE : TRUE;
if ($checked == TRUE)
  $defaults['checked'] = 'checked';

return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";

(leaving the original function form_checkbox() to co-exist. The code above could be written simpler if you don't want to pass a $data array as an argument)

And in you view files you can call this new function as follows:

// name : the name of the checkbox field
// value : the (default)value that you want to check against
// compareVal : the value that you want to compare

// Example:

$checkboxData = array(
'name'   => 'lang',
'value'  => $checkboxVal,
'compareVal'  => $compareVal
echo form_checkbox_conditional($checkboxData);

Determining the checked state by comparing values can be done as the form gets displayed or re-displayed (after validation) as long as the controller sends back the necessary variables.

Theme © iAndrew 2016 - Forum software by © MyBB