Zend表格无线电默认检查

Ale*_*tau 4 php zend-framework zend-form radio-button

我有下一个单选按钮组:

$enabled = $this->createElement('radio', 'enabled')
                ->setLabel('Enabled')
                ->setMultiOptions(array('1'=>'yes', '0'=>'no'))
                ->setValue($rank_values['enabled'])
                ->setAttrib('id', 'enabled')
                ->setAttrib('class', $action . '_enabled')
                ->setSeparator('');
Run Code Online (Sandbox Code Playgroud)

如何设置已检查的收音机?现在,当我打开我的脚本时,没有选择收音机.我想选择'是'.怎么样?

谢谢.

Fri*_*ons 8

它更容易:)

$enabled = $this->createElement('radio', 'enabled')
                ->setLabel('Enabled')
                ->setMultiOptions(array('1'=>'yes', '0'=>'no'))
                ->setValue($rank_values['enabled'])
                ->setAttrib('id', 'enabled')
                ->setAttrib('class', $action . '_enabled')
                ->setSeparator('')
                ->setValue("1");
Run Code Online (Sandbox Code Playgroud)


mar*_*ake 7

如果有人想知道,我正在使用数组表示法来声明我的表单和zend框架2中的所有元素,以便在单选按钮中选择默认选项,您必须添加属性值并使其具有关键字您希望默认选择的value_options:

// Inside your constructor or init method for your form //
$this->add(
        [
            'type'       => 'Radio',
            'name'       => 'some_radio',
            'options'    => [
                'value_options' => [
                    'opt1' => 'Radio option 1',
                    'opt2' => 'Radio option 2'
                ]
            ],
            'attributes' => [
                'value' => 'opt1' // This set the opt 1 as selected when form is rendered
            ]
        ]
    );
Run Code Online (Sandbox Code Playgroud)

我发现一些例子有点令人困惑,因为他们在值选项(0,1)中使用了数字键,所以当我看到'value'=> 1时,对我来说这是不明显的,这是value_options数组中的键.希望这有助于某人.