CakePHP : Form Radio Default Checked Value

With the CakePHP FormHelper, you can output a set or a list of radio button widgets.

Usage

<?= $form -> radio($field, $options, $attributes); ?>

Example

<?= $form -> radio('Model.field', array('Option1', 'Option2', 'Option3'), array('default' => 'option2')); ?>

In the line of code above, you’ll notice the default attribute value. This specifies the value of the radio button which will be checked by default in case there is currently no value passed through the model data.

Comments

  1. Safi on January 15, 2010

    it is not working

    Reply
  2. jeffery on March 22, 2010

    thankz very much…………

    i was having some trouble displaying the selected value for radio using cake methiod…this solved it…great tip………….
    sometimes we make a mess for even the tings that can be done easily…

    Reply
  3. Gaurav on June 29, 2010

    Thanks it worked, I was looking for an answer to this. 🙂

    Reply
  4. jack on January 11, 2011

    You can do something like this also…

    radio(‘Model.field’, array(‘option1’=>’option1’, ‘option2’=>’option2’), array(‘default’=>’option2’, ‘legend’=>false)); ?>

    Reply
  5. Uttarakhandi on February 10, 2011

    radio(‘Model.field’, array(‘option1′=>’option1′, ‘option2′=>’option2′), array(‘default’=>’option2′, ‘legend’=>false)); ?>

    Reply
  6. DaManLovett on March 4, 2011

    If you have a lot of radio buttons and they have the same values, you can set up $options = array(‘option1′=>’option1′, ‘option2′=>’option) then all you have to do is for each radio button put

    input(‘Model.field’, array(‘options’=>$options), array(‘default’ => ‘option2′)); ?>

    Reply
  7. nano on June 29, 2011

    Thanks 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: