Tuesday, February 28, 2012

Re: Can't set checkbox value to 0

The 'checked' key just checks the checkbox. If you set the value to '0' Cake (well, PHP) sees that as an empty value and therefore assigns it a value (the default being 1).

You have a few of options here:
1. Use unconventional string values like 'on' and 'off'
2. Flip the results in the model before saving
3. Make the checkboxes not inverted

Pick your poison.

On Tuesday, February 28, 2012 4:52:52 AM UTC-8, Boyan Penev wrote:
Hello,

i want to use a checkbox with inverted results: if checked- the value would be 0 and if not- 1, for which i am using the value attributes of both the checkbox and the corresponding hidden field:

$formMarkup = array('verification_needed' = array('label' => __('Don\'t verify'), 'type' => 'checkbox', 'value' => 0, 'hiddenField' => array('value' => 1), 'name' =>'Registration[verification_needed]'));
echo $this->Form->inputs($formMarkup);

However, i can't get the part whith checked = 0 working. The source of the rendered page shows that the value of the checkbox (as well as the value of the hidden field) has the value=1.
If i set any other value to the checkbox- like 555 or "yes" - it gets correctly assigned, but if i try 0- the value is always 1.

Is this behavior normal and how could i set the value to 0?

I use cake 2.1

Best regards,

Boyan

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: