2011-09-28 52 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