设置NotEmpty验证器错误消息

时间:2016-02-14 18:35:47

标签: php zend-framework2 zend-form zend-form-element zend-validate

我正在尝试使用ZF2表单和NotEmpty validaor设置自定义错误消息。

在我的表格中,我有以下无线电元素。

$this->add(array(
    'name' => 'paymentMethod',
    'type' => 'Radio',
    'options' => array(
        'label' => _('Payment Methods:'),
        'value_options' => $paymentMethods,
        'disable_inarray_validator' => TRUE,
    ),
));

在我的输入过滤器中我有

$inputFilter->add(array(
    'name' => 'paymentMethod',
    'required' => TRUE,
    'filters' => array(
        array('name' => 'Int'),
    ),
    'validators' => array(
        array(
            'name' => 'NotEmpty',
            'break_chain_on_failure' => true,
            'options' => array(
                'messages' => array(
                    NotEmpty::IS_EMPTY => _("You must select the payment method"),
                ),
            ),
        ),
        array(
            'name' => 'InArray',
            'options' => array(
                'haystacK' => $this->getPaymentMethods(),
                'messages' => array(
                    InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
                ),
            ),
        ),
    ),
));

从我的输入过滤器中可以看出,我为NotEmpty验证器设置了自定义消息。我遇到的问题是表单输出默认错误消息'值是必需的,并且不能为空'而不是我的习惯。

我认为这与“必要”有关。 => TRUE会自动设置NotEmpty验证器,但我不知道如何禁用它。

我有这个验证器的其他文本元素,它们显示自定义错误消息,这个无线电元素不是。我还有一个具有相同问题的multiheckbox元素。

有谁知道为什么会这样?

非常感谢提前。

修改

我现在遇到了另一个元素的问题,在本例中是DoctrineModule \ Form \ Element \ ObjectMultiCheckbox。

元素已定义

$this->add(array(
    'name' => 'categories',
    'type' => 'Admin\DoctrineModule\Form\Element\ObjectMultiCheckbox',
    'options' => array(
        'label' => _('Categories:'),
        'label_attributes' => array('class' => 'required'),
        'object_manager' => $this->getEntityManager(),
        'target_class' => 'Application\Entity\Categories',
        'property' => 'category',
        'is_method' => true,
        'find_method' => array(
            'name' => 'FindAll',
        ),
        'disable_inarray_validator' => TRUE,
    ),
));

并且在我的getInputFilterSpecification()方法中

...
'categories' => 
    array(
        'required' => TRUE,
        'allow_empty' => TRUE,
        'continue_if_empty' => TRUE,
        'filters' => array(
            array('name' => 'Int'),
        ),
        'validators' => array(
            array(
                'name' => 'NotEmpty',
                'break_chain_on_failure' => true,
                'options' => array(
                    'messages' => array(
                        NotEmpty::IS_EMPTY => _("You must select the items categories"),
                    ),
                ),
            ),
            array(
                'name' => 'Freedom\Zend\Validator\Doctrine\Db\DoctrineRecordExists',
                'options' => array(
                    'entityManager' => $this->getEntityManager(),
                    'entity' => 'Application\Entity\Categories',
                    'identifier' => 'catagoryId',
                    'messages' => array(
                        DoctrineRecordExists::ERROR_NO_RECORD_FOUND => _("This category does not exist"),
                    ),
                ),
            ),
        ),
    )
...

正如您所看到的,我尝试了'allow_empty''continue_if_empty'选项,但没有运气。我也试过'required' => FALSE,,但元素只是通过了验证。

默认消息显示NotEmpty验证失败。

3 个答案:

答案 0 :(得分:0)

我在代码中做了一些更改,但我没有测试。试着让我知道它是否有效。祝你好运

$inputFilter->add(array(
'name' => 'paymentMethod',
'required' => TRUE,
'filters' => array(
    array('name' => 'Int'),
),
'validators' => array(
    array(
        'name' => 'NotEmpty',
        'options' => array(
            'messages' => array(
                NotEmpty::IS_EMPTY => "You must select the payment method",
            ),
        ),
        'break_chain_on_failure' => true
    ),
    array(
        'name' => 'InArray',
        'options' => array(
            'haystacK' => $this->getPaymentMethods(),
            'messages' => array(
                InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
            ),
        ),
    ),
),

));

答案 1 :(得分:0)

我找到了解决问题的方法。 我将'required' => TRUE,选项更改为'allow_empty' => TRUE,,现在我的错误消息显示正确。

$inputFilter->add(array(
    'name' => 'paymentMethod',
    'allow_empty' => TRUE,
    'filters' => array(
        array('name' => 'Int'),
    ),
    'validators' => array(
        array(
            'name' => 'NotEmpty',
            'options' => array(
                'messages' => array(
                    NotEmpty::IS_EMPTY => _("You must select the payment method"),
                ),
            ),
            'break_chain_on_failure' => true,
        ),
        array(
            'name' => 'InArray',
            'options' => array(
                'haystacK' => $this->getPaymentMethods(),
                'messages' => array(
                    InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
                ),
            ),
        ),
    ),
));

我假设没有将所需的选项设置为true会停止自动创建NotEmpty验证器。

答案 2 :(得分:0)

修改输入过滤器会将'required' => TRUE选项更改为'required' => false

$inputFilter->add(array(
    'name' => 'paymentMethod',
    'required' => false,
    'filters' => array(
        array('name' => 'Int'),
    ),
    'validators' => array(
        array(
            'name' => 'NotEmpty',
            'break_chain_on_failure' => true,
            'options' => array(
                'messages' => array(
                    NotEmpty::IS_EMPTY => _("You must select the payment method"),
                ),
            ),
        ),
        array(
            'name' => 'InArray',
            'options' => array(
                'haystacK' => $this->getPaymentMethods(),
                'messages' => array(
                    InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
                ),
            ),
        ),
    ),
));