How to add file element in zend form? [duplicate]
I am creating forms like this:
UploadFiale.php
<?php
class Form_UploadFile extends Zend_Form {
public function __construct( $options = null ) {
parent::__construct( $options );
$this->setMethod('post');
$elements = array();
// photo
$element = new Zend_Form_Element_File('photo');
$element->setLabel('Photo');
$element->addValidator('Count', false, 1);
$element->addValidator('Size', false, 102400);
$element->addValidator('Extension', false, 'jpg,png,gif');
$elements[] = $element;
// submit button
$element = $this->CreateElement('submit', 'submit');
$element->setLabel('Save');
$elements[] = $element;
$this->addElements( $elements );
$this->setElementDecorators( array( 'ViewHelper' ) );
$this->setDecorators( array( array( 'ViewScript', array( 'viewScript' => 'uploadfile-form.phtml' ) ) ) );
} // end construct
} // end class
?>
uploadfile-form.phtml
<form action=<?= $this->element->getAction() ?> method=<?= $this->element->getMethod() ?> id='AddNotificationForm' enctype='multipart/form-data' class='AjaxForm'>
<table border='0'>
<tr>
<td><label><?= $this->element->photo->getLabel() ?></label></td>
<td><?= $this->element->photo; ?></td>
</tr>
<tr>
<td></td>
<td><?= $this->element->submit; ?></td>
</tr>
</table>
</form>
When I init开发者_开发知识库ialize form class and render it in view then I am getting following error:
Warning: No file decorator found... unable to render file element in /project_folder/library/Zend/Form/Element.php on line 2041
Same structure is working with other form elements but not working with file element. Can someone tell me that what is wrong here ?
Thanks
Zend_Form_Element_File is special in a way that it does not work without "File" Decorator either you add it specifically or remove
$this->setElementDecorators( array( 'ViewHelper' ) );
精彩评论