简体   繁体   English

将自定义验证错误消息逐个元素合并到表单对象中

[英]Incorporate custom validation error messages into form object by element

I have the following code which creates a specific text element: 我有以下代码创建一个特定的文本元素:

       $this->add([           
            'type'  => 'text',
            'name' => 'newpassword',
            'attributes' => [
                'id' => 'newpassword',
                'class' => 'form-control'
            ],
            'options' => [
                'label' => 'Enter New User Password',
            ],
        ]);

And I have the following code which produces my input filter definitions: 我有以下代码可生成我的输入过滤器定义:

            $inputFilter->add([
                    'name'     => 'newpassword',
                    'required' => true,
                    'filters'  => [
                        ['name' => 'StringTrim'],
                        ['name' => 'StripTags']                 
                    ],
                    'validators' => [
                        [
                            'name'    => 'StringLength',
                            'options' => [
                                'min' => 6,
                                'max' => 256
                            ],
                        ]                   
                    ],
            ]);       

What I want to accomplish is adding my custom messages. 我要完成的工作是添加自定义消息。 Here's the way they have it in the api documentation: 这是他们在api文档中拥有它的方式:

$validator = new Zend\Validator\StringLength(array('min' => 8, 'max' => 12));

$validator->setMessages( array(
    Zend\Validator\StringLength::TOO_SHORT =>
    'The string \'%value%\' is too short',
    Zend\Validator\StringLength::TOO_LONG  =>
    'The string \'%value%\' is too long'
));

How do I incorporate my custom validation messages into my already programmed code? 如何将自定义验证消息合并到已经编程的代码中?

UPDATE: 更新:

I think this is where i will find success, but not sure how to do it: 我认为这是我会找到成功的地方,但不确定如何做到:

$inputFilter->get('newpassword')->getValidatorChain()->

Use this-: its messageTemplates to set custom message 使用此-:其messageTemplates设置自定义消息

$inputFilter->add([
            'name'     => 'newpassword',
            'required' => true,
            'filters'  => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ],
            'validators' => [
                [
                    'name'    => 'StringLength',
                    'options' => [
                        'min' => 6,
                        'max' => 256,
                        'messageTemplates'=>array(
                            \Zend\Validator\StringLength::TOO_SHORT =>
                                'The string \'%value%\' is too short',
                            \Zend\Validator\StringLength::TOO_LONG  =>
                                'The string \'%value%\' is too long'
                        )
                    ],
                ]
            ],
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM