Hello,

I'm having a problem with setting the default value of a checkbox with
form helper.

The version of CakePHP I've tried on is cake_1.2.0.7962 (final of 1.2)
and cake_1.2.0.7692-rc3. (Both gave the same results.)

I believe that the $options['default'] should set the default value to
be set when the value from the database is not set. So I'm expecting
the following code to create a checkbox checked by default, either
when the data from the database is not set, or the data from the
database is set to 1. (I don't want it to be checked by default when
the data from the database is set to 0.)

$form->input('Model.field_name', array('default' => 1));

However, this outputs a checkbox that is not checked.

I also tried the following:

$form->input('Model.field_name', array('checked' => true));

This results in a checkbox that is checked, EVEN WHEN the data from
the database is set to 0.

I've explored the source code around form helper and made 2 changes to
make it work as I expect.

1) cake/libs/view/helper.php: line 589 (function value)

if (empty($result) && isset($options['default'])) {
    $result = $options['default'];
}

This code overwrites the value from the database by $options
['default'] when the value is evaluated as "empty", which is not
desired when the value 0 or "0" is stored in the database with an
intention.

I changed the line to:

if ($result == null && isset($options['default'])) {
    $result = $options['default'];
}

This uses the value from the database even when it is set to be 0 or
"0", and uses the default value when the value is not set in the
database. (This fix is effective for other form elements than
checkbox.)

2) cake/libs/view/helpers/form.php: line 839 (function checkbox)

} elseif (!empty($value) && $value === $options['value']) {
    $options['checked'] = 'checked';
}

I could not understand the intention of this code, but it sure does
NOT check the checkbox when the default value is set to 1.

I changed this line to the following as an experiment:

} else if ($options['value'] == 1) {
    $options['checked'] = 'checked';
}

Now it works as I have expected: When I set 'default' => 1, the
checkbox is checked when the value is not set in the database, or the
value from the database is set to 1, and NOT checked when the value
from the database is set to 0.

With the 2 changes above, I got the results I have expected, and in my
application, it seems like working well, without any other side
effects.

I'm pretty confident that the 1st one (in helper.php) is a bug, but
I'm not sure about the 2nd (in form.php), as I don't understand what
the condition is intended for... :(

My questions are:

a) Is anyone experiencing the same problem?    or

b) What the condition in line 839 in form.php is intended for?    or

c) What is the collect way to fix this problem?    or

d) Am I misunderstanding the spec of 'default' option??


By the way, another way of working around this is of course to write a
lot of code to decide the checked state to pass to the $from->input
function, but it's not a permanent solution and that's not what we
want to do here.

Thanks.
Kizuki


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to