Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP creating radio buttons

Tags:

cakephp

How can I create two radio buttons with one being preselected based on the value of $foo? The snippet below creates them fine but does not select either of the two buttons.

$options = array('standard' => ' Standard','pro' => ' Pro');
$attributes = array(
    'legend' => false,
    'value' => false,
    'checked'=> ($foo == "pro") ? FALSE : TRUE,
);
echo $this->Form->radio('type',$options, $attributes);
like image 913
stef Avatar asked Feb 09 '12 10:02

stef


1 Answers

It's simple.. use the default value to $foo:

$options = array(
    'standard' => 'Standard',
    'pro' => 'Pro'
);

$attributes = array(
    'legend' => false,
    'value' => $foo
);

echo $this->Form->radio('type', $options, $attributes);

As you can see on the documentation:

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::radio

like image 106
Thiago Belem Avatar answered Oct 01 '22 04:10

Thiago Belem