设置非空验证器错误消息


Set NotEmpty validator error message

我正在尝试使用 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 验证器有关,但我不知道如何禁用它。

我还有其他带有此验证器的文本元素,它们可以很好地显示自定义错误消息,而此单选元素则没有。 我还有一个具有相同问题的多复选框元素。

有谁知道为什么会这样?

提前非常感谢。

编辑

我现在对另一个元素有同样的问题,在这种情况下,它的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,但该元素只是通过了验证。

当非空验证失败时,将显示默认消息。

修改输入过滤器 将'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"),
                ),
            ),
        ),
    ),
));

我对你的代码进行了一些更改,但我没有测试。尝试让我知道它是否有效。祝你好运

$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"),
            ),
        ),
    ),
),

((;

我已经找到了解决问题的方法。我将'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 验证器。