• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkboxes, checked by default, how to ?

#1
[eluser]Référencement Google[/eluser]
Hi,

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:
Code:
<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 ?

Christophe

#2
[eluser]jgsd[/eluser]
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):

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

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

#3
[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 ?

#4
[eluser]jgsd[/eluser]
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!

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

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

Code:
if(count($_POST)){
// is a post - do something
}else{
// is not posted make checkbox checked
}

#7
[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.

#8
[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:
http://ellislab.com/forums/viewthread/56960/

Christophe

#9
[eluser]moonbeetle[/eluser]
You could extend the form helper with an additional function as follows:

Code:
/**
* 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;
  unset($data['compareVal']);
}
if ($checked == TRUE)
  $defaults['checked'] = 'checked';
else
  unset($defaults['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:

Code:
// 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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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