2
votes

hi i am using a zend image element in my project , i have validated the field . if i choose other than an image and submit the form , a default error message is displayed , i tried to remove it and add a custom message by

    $image->removeDecorator('Error');
    $image->addErrorMessage('Invalid image');

but it is not working . the error message is

File '.zfproject.xml' is no image, 'application/xml' detected

how can i add a custom error message and remove the default error meassage , please help ....

this is my code

    $image = new Zend_Form_Element_File('image');
    $image->setLabel('Image URL :');
    $image->setDestination($imagePath);
    $image->addValidator('IsImage', false);
    //$image->removeDecorator('Error');
    //$image->addErrorMessage('tester');
2

2 Answers

1
votes

The validator message is stored in Zend/Validate/File/IsImage.php here:

    protected $_messageTemplates = array(
    self::FALSE_TYPE   => "File '%value%' is no image, '%type%' detected",
    self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
    self::NOT_READABLE => "File '%value%' is not readable or does not exist",
);

The easiest way around this would be subclass the validator:

class Custom_Validate_File_IsImage extends Zend_Validate_File_IsImage
{
    /**
     * @var array Error message templates
     */
    protected $_messageTemplates = array(
        self::FALSE_TYPE   => "Customize your message to whatever you want to right here.",
        self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
        self::NOT_READABLE => "File '%value%' is not readable or does not exist",
    );
}

and use the custom validator instead with :

$image->addValidator(new Custom_Validate_File_IsImage());
0
votes

If you don't care about the different validation rule error messages, and only want to display a general error failure message, you can simply do

$element->addErrorMessage('Your error message');