PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Validation Messages

Each validator which is based on Zend_Validate provides one or multiple messages in the case of a failed validation. You can use this information to set your own messages, or to translate existing messages which a validator could return to something different.

These validation messages are constants which can be found at top of each validator class. Let's look into Zend_Validate_GreaterThan for an descriptive example:

<?php
protected $_messageTemplates = array(
    
self::NOT_GREATER => "'%value%' is not greater than '%min%'",
);

As you can see the constant self::NOT_GREATER refers to the failure and is used as key, and the message itself is used as value of the message array.

You can retrieve all message templates from a validator by using the getMessageTemplates() method. It returns you the above array which contains all messages a validator could return in the case of a failed validation.

<?php
$validator 
= new Zend_Validate_GreaterThan();
$messages  $validator->getMessageTemplates();

Using the setMessage() method you can set another message to be returned in case of the specified failure.

<?php
$validator 
= new Zend_Validate_GreaterThan();
$validator->setMessage(
    
'Please enter a lower value',
    
Zend_Validate_GreaterThan::NOT_GREATER
);

The second parameter defines the failure which will be overridden. When you omit this parameter, then the given message will be set for all possible failures of this validator.

Zend Framework