Zend framework 如何设置Zend_表单无线电元素的默认选中值?

Zend framework 如何设置Zend_表单无线电元素的默认选中值?,zend-framework,zend-form,Zend Framework,Zend Form,我有一个无线电元件,有两个选择。我想设置一个作为默认值,以防用户忘记检查它。我该怎么做 解决方案: 将setValue与键一起使用。例如: $enablestate=new Zend_Form_Element_Radio('enablestate'); $enablestate->addMultiOptions(array('enabled'=>'Enabled','unenabled'=>'Unenabled')); $enablestate->setSeparator

我有一个无线电元件,有两个选择。我想设置一个作为默认值,以防用户忘记检查它。我该怎么做

解决方案:
将setValue与键一起使用。例如:

$enablestate=new Zend_Form_Element_Radio('enablestate');
$enablestate->addMultiOptions(array('enabled'=>'Enabled','unenabled'=>'Unenabled'));
$enablestate->setSeparator('')->setValue('enabled');
这段代码应该可以做到这一点

$radio = new Zend_Form_Element_Radio('radio');
$radio->addMultiOption("option1", "option1")->setAttrib("checked", "checked");
$radio->addMultiOption("option2", "option2");
$this->addElement($radio);
有关更多阅读资料,请参阅:


使用元素对象时,我就是这样做的

$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);
$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);