ZF2 - 需要在特定条件失败时显示特定错误消息 [英] ZF2 - Need to display specific error message on specific condition failure

查看:26
本文介绍了ZF2 - 需要在特定条件失败时显示特定错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ZF2 表单验证.我必须验证两个字段 USERNAME 和 PASSWORD.一切正常,但我收到类似

I am using ZF2 form validation.I have to validate two fields USERNAME and PASSWORD. everything is working fine but I am getting message like

Please enter username.
Username can not be less than 3 characters.

Please enter password.
Password can not be less than 6 characters.

如果用户未输入任何值,则应仅显示此消息

If user is not entering any value then only this message should display

Please enter username.    
Please enter password.

我不想在失败时在字段上显示所有错误消息.

I don't want do display all the error messages on a field on failure.

提前致谢.

推荐答案

我得到了答案:为了打破 ZF2 中的验证链,我们必须使用

I got the answer : In order to break the validation chain in ZF2 , we have to use

'break_chain_on_failure' => 真

'break_chain_on_failure' => true

$this->add(
    array(
        'name'       => 'usernmae',
        'required'   => true,
        'filters'    => array(
            array('name' => 'Zend\Filter\StringTrim')
        ),
        'validators' => array(
                          array('name' => 'NotEmpty', 
                            'options' => array('encoding' => 'UTF-8', 
                                'messages' => array(
                                    NotEmpty::IS_EMPTY => 'Please enter username')), 
                            'break_chain_on_failure' => true), 
            array(
                'name'    => 'Zend\Validator\StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 3,
                    'max'      => 30,
                    'messages' => array(
                                    StringLength::TOO_LONG => 'Username can not be more than 30 characters long', 
                                    StringLength::TOO_SHORT => 'Username can not be less than 3 characters.')
                ),
                'break_chain_on_failure' => true
            )
        )
    )
);

我的博客:http://programming-tips.in

这篇关于ZF2 - 需要在特定条件失败时显示特定错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆