如何配置ObjectMultiCheckBox以保留空元素?

问题描述 投票:0回答:1

我的表单中有以下元素,并且我尝试了在网络中找到的所有可能选项以允许元素为空值:

     $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',

        'name' => 'directPractice',

        'options' => array(
            'label' => 'A. Check all direct practice field education assignments',
            'label_attributes' => array(
                'class' => 'label-multicheckbox-group'
            ),

            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => false,

            'object_manager' => $this->getObjectManager(),
            'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
            'property' => 'directPractice', //'your db column name'
            'disable_inarray_validator' => true,
            'value_options' => array(
                '1' => 'Adults',
                '2' => 'Individuals',
                '3' => 'Information and Referral',
                '4' => 'Families',
                array(
                    'value' => 'Other',
                    'label' => 'Other (specify)',
                    'label_attributes' => array(
                        'class' => 'bindto',
                        'data-bindit_id' => 'otherDirectPracticeTxt_ID'
                    ),
                    'attributes' => array(
                        'id' => 'otherDirectPractice_ID',
                    ),
                )
            ),
        ),
        'attributes' => array(
            'value' => '1', //set checked to '1'
            'multiple' => true,

        )
    ));

当它为空时,我总是收到相同的错误消息:

验证失败“directPractice”:数组 ( [isEmpty] => 值为必填项且不能为空 )

php zend-framework doctrine-orm zend-framework2 zend-form
1个回答
0
投票

好吧,这是我研究后能想到的最好的办法。解决方案的灵感来自这些问题

表单:隐藏字段和ObjectMultiCheckBox

 public function init()
    {

        $this->setAttribute('method', 'post');
        $this->setAttribute('novalidate', 'novalidate');

        //hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
        $this->add(array(
            'type' => 'Hidden',
            'name' => 'otherLearningExperiences[]', // imitates checkbox name
            'attributes' => array(
                'value' => null
            )
        ));
        $this->add(array(
//            'type' => 'Zend\Form\Element\MultiCheckbox',
            'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
            'name' => 'otherLearningExperiences',
            'options' => array(
                'label' => 'C. Check other learning experiences',
                'object_manager' => $this->getObjectManager(),
                'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
                'property' => 'otherLearningExperiences',

                'label_attributes' => array(
                    'id' => 'macro_practice_label',
                    'class' => 'control-label label-multicheckbox-group'
                ),
                'value_options' => array(
                    array(
                        'value' => 'seminars',
                        'label' => 'Seminars, In-Service Training/Conferences',
                        'label_attributes' => array(
                            'class' => 'bindto',
                            'data-bindit_id' => 'seminarsTxt_ID'
                        ),
                        'attributes' => array(
                            'id' => 'seminars_ID',
                        ),
                    ),
                    array(
                        'value' => 'other',
                        'label' => 'Other (specify)',
                        'label_attributes' => array(
                            'class' => 'bindto',
                            'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
                        ),

                        'attributes' => array(
                            'id' => 'otherLeaningExperiences_ID',
                        ),
                    )
                ),
            ),
            'attributes' => array(
                //'value' => '1', //set checked to '1'
                'multiple' => true,

                'empty_option' => '',
                'required' =>false,
                'allow_empty' => true,
                'continue_if_empty' => false,
            )
        ));

验证器

      $inputFilter->add($factory->createInput(array(
            'name'     => 'otherLearningExperiences',
            'required' => false,
            'allow_empty' => true,
            'continue_if_empty' => true,

        )));
}

查看

//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');
© www.soinside.com 2019 - 2024. All rights reserved.