2011-09-28 54 views
6

我有问题,下面的Zend表单抛出一个错误。 问题是“文件”元素和使用setElementDecorators。Zend文件上传和元素装饰器

class Products_AddForm extends Zend_Form 
{ 
    function init() { 

     // other form elements... 

     $uploadElement = new Zend_Form_Element_File('Excel'); 
     $uploadElement->setLabel('Excel'); 
     $this->addElement($uploadElement); 

     $this->setElementDecorators(array(
      'ViewHelper', 
      'Errors', 
      array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
      array('Label', array('tag' => 'th')), 
      array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
     )); 



    } 
} 

这会引发错误。

(Warning: Exception caught by form: No file decorator found... unable to render file element Stack Trace: #0) 

SetElementDecorators后,在结尾处增加$uploadElement->addDecorator('File');会的工作,但是这会给我的文件元素的两倍!

有人可以帮忙吗?

TIA 马特

回答

10

文件元素需要它自己的装饰 - Zend_Form_Decorator_File。

$this->setElementDecorators(array(
     'File', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
     array('Label', array('tag' => 'th')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
)); 

[编辑]

刚才已经注意到,您还使用其他形式的元素。

原密码后,添加:

$this->getElement('Excel')->setDecorators(
    array(
     'File', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
     array('Label', array('tag' => 'th')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
    ) 
); 

这样,视图助手添加到所有其他元素,并为您的文件元素的文件来代替。

+0

感谢你的帮助。添加这个抛出:警告:由窗体捕获的异常:方法getMaxFileSize不存在堆栈跟踪:#0 – frgtv10

+0

已更新我的回答:) –

+0

工作。 havnt在zend的文档中注意到了这一点!? :/ 谢谢! – frgtv10