模型zend 2中的多选验证

时间:2016-08-10 09:32:15

标签: php

我有一个带有多选框的zend 2表单,用户可以一次选择多个项目。当用户没有选择任何选项时,系统会给出错误,因为我已添加了必要的验证。现在,当用户不选择任何值时,我想更改默认消息“值是必需的,不能为空”。以下是2个代码。一个是模型,另一个是形式。

FORM:

    $this->add(array(
                'name' => 'user_group',
                'type' => 'Select',
                'options' => array(
                    'label' => 'User Group',
                    'value_options' => $parentUsers
                ),
                'attributes' => array(
                    'id' => "user_group",
                    'multiple' => 'multiple',
                    'class' => "chosen-select",
                    'data-placeholder' => "Type Group Name Here",
                    'style' => "width:350px;",
                    'value' => $selectedUserRoles,
                )
            ));

Model:

     $inputFilter->add(array(
                    'name' => 'user_group',
                    'required' => true,
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                     \Zend\Validator\NotEmpty::IS_EMPTY => 'Please select user group.',
                                ),
                            ),
                            'break_chain_on_failure' => true,
                        ),
                    ),
                ));

    I want if user did not select any group from Multi Select, it gives error with custom message "Please select any group". Currently, I just set required validation but it didn't work for me.

0 个答案:

没有答案