簡體   English   中英

由formInput進行的zf2復選框渲染失敗

[英]zf2 Checkbox rendering by formInput fails

我想制作一個使用條款的自動檢查箱。 我發現了一個有趣的解決方案,由網絡編碼器提供。 如何驗證ZF2中的復選框

情況是我有一個困難的HTML結構,這就是為什么我既不能使用formRow也不可以使用formCollection進行渲染的原因。 我正嘗試使用以下方法:

$agreement = $form->get('agreement');
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);

我收到空值:

<input type="checkbox" name="agreement" class="checkbox" value="">

我也嘗試添加隱藏字段,但是我的嘗試也失敗了:

$agreement = $form->get('agreement');
echo $this->formHidden($agreement);
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);

的HTML是:

<input type="hidden"   name="agreement" class="checkbox" value="">
<input type="checkbox" name="agreement" class="checkbox" value="">

結果,我收到有關“值是必需的,不能為空”的錯誤

可能有人可以給我一個提示,說明如何正確顯示我的情況下的復選框?

我的inputFilter的代碼:

<?php
namespace Auth\Form;

use Zend\InputFilter\InputFilter;
use Zend\Validator\Digits;

class UserFormFilter extends InputFilter {

    public function __construct() {

        $this->add(array(
            'name' => 'agreement',
            'validators' => array(
                array(
                    'name' => 'Digits',
                    'break_chain_on_failure' => true,
                    'options' => array(
                        'messages' => array (
                            Digits::NOT_DIGITS => 'You must agree to the terms of use.',
                        )
                    ),
                ),

            ),
        ));
    }
}
?>

請檢查一下。

$this->add(array(
        'type' => 'Zend\Form\Element\Checkbox',
        'name' => 'agreeterms',
        'options' => array(
            'label' => 'I agree to all terms and conditions',
            'use_hidden_element' => true,
            'checked_value' => 1,
            'unchecked_value' => 'no'
        ),
        'attributes' => array(//the difference is here
            'value' => 'yes'
        )
    ));

如果添加屬性數組,它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM