Andrew
Andrew

Reputation: 238747

How to set the default checked value of a Zend_Form Radio Element?

I have a radio element with two options. I want to set one as the default value in case the user forgets to check it. How can I do this?

Solution:

$this->addElement('radio', 'choose', array(
    'required'   => true,
    'multiOptions' => array(
        'yes' => 'heck yes',
        'no' => 'please no'
    ),
    'value' => 'yes' //key of multiOption
));

Upvotes: 4

Views: 16962

Answers (3)

dejjub-AIS
dejjub-AIS

Reputation: 1541

This is how I do when I use element objects

$myRadio = new Element\Radio('myradio');
$myRadio->setLabel("Are you mad?");
$myRadio->setValueOptions(array('0'=>'Yes', '1' =>'No', '2'=>'Maybe'));
$myRadio->setValue('2'); //set it 0 if you sure you know it
$this->add($myRadio);

Upvotes: 0

Mark Basmayor
Mark Basmayor

Reputation: 2569

this code should do it

$radio = new Zend_Form_Element_Radio('radio');
$radio->addMultiOption("option1", "option1")->setAttrib("checked", "checked");
$radio->addMultiOption("option2", "option2");
$this->addElement($radio);

for further readings you can refer to:

ZendFramework manual

http://www.w3schools.com/html/html_forms.asp

Upvotes: 1

timpone
timpone

Reputation: 19969

use setValue with key. For example:

$enablestate=new Zend_Form_Element_Radio('enablestate');
$enablestate->addMultiOptions(array('enabled'=>'Enabled','unenabled'=>'Unenabled'));
$enablestate->setSeparator('')->setValue('enabled');

Upvotes: 7

Related Questions