10

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.

Zend\Validator\Exception\InvalidArgumentException

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

3 Answers 3

22

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(
                                \Zend\Validator\NotEmpty::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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, where are tje possible keys ("stringLengthTooShort", \Zend\Validator\NotEmpty::IS_EMPTY, "stringLengthTooLong") documented?
3

You also can set the error message of inputFilter so:

    $form = new ParticipantForm();  
    $mailInput = new Input('mail');
    $mailInput->setRequired(true);
    $mailInput->setErrorMessage("Empty input");

Comments

1

The StringLength validator does not check for the input to be empty or not. It checks against lengths. Following message templates exist for StringLength validator:

const INVALID   = 'stringLengthInvalid';
const TOO_SHORT = 'stringLengthTooShort';
const TOO_LONG  = 'stringLengthTooLong';

/**
 * @var array
 */
protected $messageTemplates = array(
    self::INVALID   => "Invalid type given. String expected",
    self::TOO_SHORT => "The input is less than %min% characters long",
    self::TOO_LONG  => "The input is more than %max% characters long",
);

See the example of @Developer for a direct approach. Though i suggest going with the CamelCased naming of the Validators, so 'name' => 'NotEmpty' instead of 'name' => 'not_empty'

You can check which messageTemplates exist if you see the code for each of the validator classes. You will find them under ./vendor/zendframework/zendframework/library/Zend/Validator/*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.