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

时间:2015-03-05 03:57:47

标签: php zend-framework doctrine-orm zend-framework2 zend-form

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

     $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 collumn 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] =>价值是必需的,不能为空   )

1 个答案:

答案 0 :(得分:0)

好的,这是我研究之后可以提出的最好的方法。解决方案的灵感来自这些question

表单:隐藏字段和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');