Zend File Upload and Element Decorators

末鹿安然 提交于 2019-12-28 04:31:07

问题


I have the problem, that the following Zend Form throws an error. The problem is the "file"-element and using setElementDecorators.

    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'))
        ));



    }
}

This throws an error.

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

Adding $uploadElement->addDecorator('File'); at the end after the SetElementDecorators will work, but this will give me the file element twice!

Can anybody help, please?

TIA Matt


回答1:


The File element requires it's own decorator - 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'))
));

[edit]

Have just noticed that you are also using other form elements.

After your original code, add:

$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'))
    )
);

That way, ViewHelper is added to all other elements, and for your File element File is used instead.



来源:https://stackoverflow.com/questions/7580839/zend-file-upload-and-element-decorators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!