Issues

ZF-11763: Zend_Form_Element_File addErrorMessage is set, but rendering as File 'element name' was not uploaded

Description


$this->setMethod("post")
    ->setEnctype('multipart/form-data')
    ->setAction("");

$fileDecorator = array(
    'File',
    'Description',
    'Errors',
    array(array('data' => 'HtmlTag'), array('tag' => 'td')),
    array('Label', array('tag' => 'td')),
    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
);


$photo = new Zend_Form_Element_File("photo");
$photo->setLabel("Upload Photo")
    ->setRequired(true)
    ->addErrorMessage('Please choose an image file to upload')
    ->setDecorators($fileDecorator);

after validation, error message is showing up as "File 'photo' was not uploaded"

Comments

{{addErrorMessage}} doesn't override error messages provided by the element, it just adds additional messages to the stack.

I haven't tried it myself recently, but I believe you will have to set up your own instance of {{Zend_Validate_File_Upload}}, override the appropriate error message(s) there, and then inject that into {{Zend_Form_Element_File}} via method {{setValidators}}

I would like to check this myself.

First I am not your opinion that you must override an file validator to set messages. Second you can't override the upload validator as it is an fixed validator of the file element. Without it no upload would work.

Simple solution: Don't use the custom error messages ({{addErrorMessage}}). Use the validator and the „setMessage“ method:


$form->addElement(
    'file',
    'photo',
    array(
        'label'    => 'Photo',
        'required' => true,
    )
);
$form->getElement('photo')->getValidator('Upload')->setMessage(
    'Please choose an image file to upload',
    Zend_Validate_File_Upload::NO_FILE
);