Issues

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

Issue Type: Bug Created: 2011-09-22T14:55:47.000+0000 Last Updated: 2012-07-30T10:00:07.000+0000 Status: Closed Fix version(s): Reporter: Arun Vasudevan Nair (aruntechguy) Assignee: Frank Brückner (frosch) Tags: - Zend_Form

  • zend_form

Related issues: Attachments:

Description

<pre class="highlight">
$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

Posted by Adam Lundrigan (adamlundrigan) on 2011-09-23T01:39:43.000+0000

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

Posted by Thomas Weidner (thomas) on 2011-10-28T04:49:24.000+0000

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.

Posted by Frank Brückner (frosch) on 2012-07-30T09:58:39.000+0000

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

<pre class="highlight">
$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
);

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts