The Zend Validator error messages are not the most helpful. Considor the below code for creating a file input:

$this->addElement('file', 'signature', array(
    'validators' => array(
        array('Size', false, 20480), //20k
        array('Extension', false, 'png'),
    'required' => false,
    'label' => 'Signature',
    'destination' => SIG_PATH,

For instance, on the above file input, if you attempt to upload something that is not a PNG file (i.e. photo.JPG, the message will be:

File 'photo.JPG' has a false extension

That’s not very user friendly as it doesn’t give the user any indication of what is an “allowed” file type.

So, to combat this, create separate Zend Validators and attach them to the Form Elements separately:

$this->addElement('file', 'signature', array(
    'required' => false,
    'label' => 'Signature',
    'destination' => SIG_PATH,
    'maxfilesize' => 20480,

$fileSize = new Zend_Validate_File_Size(20480);
$fileSize->setMessage('Please only upload a picture with a maximum 20kb file size');

$fileType = new Zend_Validate_File_Extension('png');
$fileType->setMessage('Only .png files are acceptable for signatures.');

$sig = $this->getElement('signature');
$sig->addValidators(array($fileSize, $fileType));

This way you have total control over the messages sent to the user. And after all, user experience is key these days.

By admin

3 thought on “Custom Zend Validator error messages”
  1. Also you can extend built-in validators (I would really recommend that) for your own error messages like this (I needed this because of translations into another locales):

    class Validate_NotEmpty extends Zend_Validate_NotEmpty {

    public function __construct() {
    $this->_messageTemplates = array(
    self::IS_EMPTY => __(“hodnota je vyžadována a nesmí být prázdná”), //values is required and must not be empty
    self::INVALID => __(“chybný typ hodnoty (float, string, nebo integer)”)
    ); //invalid value type (float, string or integer)

    Always check $this->_messageTemplates in the class you are extending so that you cover all possible states (there might be more of them).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.