Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

zend framework2 - zendframework 2 inputfilter customize default error message

I am trying to customize the default error message "Value is required and can't be empty" in zf2

I am using following code to add customise default error message in validators of inputfilter

$inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'isEmpty' => 'Please enter User Name between 4 to 20 character!' 
                            ),                            
                        ),
                    ),
                ),
            )));

But I am getting following error.

ZendValidatorExceptionInvalidArgumentException

File:
    /home/website/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php:220

Message:
    No message template exists for key 'isEmpty'

What I am doing wrong?

reference

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this

$inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                      'name' =>'NotEmpty', 
                        'options' => array(
                            'messages' => array(
                                endValidatorNotEmpty::IS_EMPTY => 'Please enter User Name!' 
                            ),
                        ),
                    ),
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!', 
                                'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!' 
                            ),
                        ),
                    ),
                ),
            )));

reference

Other validator set


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...