CakePHP creating radio buttons

you should preselect the value for any form field from the controller

@see http://www.dereuromark.de/2010/06/23/working-with-forms/ "Default Values"


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


This is the way to go

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

    if($foo === 'pro') {
       $attributes['default'] = 'pro';
    }

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

A better Solution is to set the defaults in the controller as Mark has pointed. That way you can set defaults at the end of your controller's action like...

Let's assume your Model is Member with membership_type field

   $this->data['Member']['membership_type '] = 'pro';

Tags:

Cakephp