How does $form->checkbox() really work?

Permalink
Based on the documentation, I expected that this would render a checked checkbox:

$form->checkbox('dedup_addresses',true,true);


But this does not render a checkbox in a checked state. Instead changing the "value" to false caused the checkbox to be checked by default - like so:

$form->checkbox('dedup_addresses',false,true);


The problem with this is that the value being rendered is now "false" so testing for this in the controller is more difficult. Am I missing something? The only way I could get this to work was to do the following:

$form->checkbox('dedup_addresses',true,true,array('checked'=>'checked'));


Thanks!

alexaalto
 
JohntheFish replied on at Permalink Reply
JohntheFish
I am surprised your first example did not work. I have never done it that way, but its close to what I would expect to work.

My usual code gives a checkbox a string value, such as:
$form->checkbox('dedup_addresses','dedup_addresses' ,$dedup_addresses);


Where $dedup_addresses is the value provided from the controller (ie saved last time).
So in essence:
$dedup_addresses = 'dedup_addresses'; // because that is what got returned before.
...
...
...
$form->checkbox('dedup_addresses','dedup_addresses' ,$dedup_addresses);