Zend Framework 2复选框

时间:2014-11-26 13:10:20

标签: php checkbox zend-framework2

我对ZF2很新,我有一个非常奇怪的问题。我使用数组创建了一个简单的复选框 并将选中的值设置为良好和未选中的值为坏。但是当我提交表单时,URL显示当选中复选框时,它会发送..... checkbox = bad& checkbox = good ...我不知道为什么。

class SearchForm extends Form {
public function __construct($name = null){
    parent:: __construct('Search');
    $this->setAttribute('method', 'get');
    $this->setAttribute ( 'enctype', 'multipart/formdata' );
    $this->add(array(
            'type' => 'Zend\Form\Element\Checkbox',
            'name' => 'checkbox',
            'options' => array(
                    'label' => 'A checkbox',

                     'checked_value' => 'good',
                     'unchecked_value' => 'bad',


            ),

    ));

2 个答案:

答案 0 :(得分:3)

因为Zend\Form\Element\Checkbox默认use_hidden_elementtrue

  

如果设置为true(默认值),则视图助手将生成一个   包含未经检查的值的隐藏元素。因此,何时   使用自定义未选中的值时,必须将此选项设置为true。

您使用GET方法。当然,您会在查询字符串中看到两个值:对于复选框和隐藏元素。

更详细地查看ZF2#Checkbox

答案 1 :(得分:0)

不是100%肯定这是问题的答案,而不是现在可以测试的位置,但它很可能与复选框使用的隐藏元素一起尝试:

$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
    'options' => array(
        'label' => 'A checkbox',
        'use_hidden_element' => false,
        'checked_value' => 'good',
        'unchecked_value' => 'bad'
    ),
));
相关问题