Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.6.0RC1, 1.6.0RC2, 1.6.0RC3
-
Fix Version/s: 1.6.1
-
Component/s: Zend_File_Transfer, Zend_Form
-
Labels:None
Description
It is not possible at the moment to create an upload element. When I set required = false, the file is not uploaded and there is no validation. When I set required = true a file is needed. It is not possible to set the form element optional.
Requirements for my form:
Image is optional.
Image are upload ->validate this.
Image not uploaded -> ignore image.
Here's my config ini:
news.create.elements.image.type = "file"
news.create.elements.image.options.label = "Bild"
news.create.elements.image.options.validators.size.validator = "Size"
news.create.elements.image.options.validators.size.options = "20MB"
news.create.elements.image.options.validators.mimetype.validator = "Mimetype"
news.create.elements.image.options.validators.mimetype.options = "image/gif,image/jpeg,image/png"
news.create.elements.image.options.required = false
My php:
$config = Zend_Registry::get ('config');
$form = new My_Form ($config->news->create);
$form->image->setDestination (ROOT_DIR . '/public/images/neuigkeiten');
if ($this->_request->isPost ()) {
$formData = $this->_request->getPost ();
if ($form->isValid ($formData)) {
...
} else {
$form->populate ($formData);
}
}
To validate only the file we are interesting in ( $this->getName() ) and return true if :
1 : the validation only failed because no file were uploaded (Zend_Validate_File_Upload::NO_FILE )
AND
2 : the file were not required (isRequired() == false)
Also clean the error ( $adapter->clearErrors() ) so no error message will be displayed on the form for a non required file input.