如何设置Zend_Form无线电元素的默认选中值?

时间:2009-12-15 23:42:58

标签: zend-framework zend-form

我有一个有两个选项的无线电元素。我想将一个设置为默认值,以防用户忘记检查它。我怎么能这样做?

解决方案:

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

2 个答案:

答案 0 :(得分:7)

使用带键的setValue。例如:

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

答案 1 :(得分:1)

此代码应该这样做

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

如需进一步阅读,请参阅:

ZendFramework manual

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

相关问题