本文介绍了Zend 文件上传和元素装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,以下 Zend 表单会引发错误.问题是文件"元素和使用 setElementDecorators.

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



    }
}

这会引发错误.

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

SetElementDecorators 之后添加 $uploadElement->addDecorator('File'); 会起作用,但这会给我两次文件元素!

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

有人可以帮忙吗?

TIA马特

推荐答案

File 元素需要它自己的装饰器 - Zend_Form_Decorator_File.

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

刚刚注意到您还在使用其他表单元素.

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

这样,ViewHelper 被添加到所有其他元素,而您的 File 元素使用 File 来代替.

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

这篇关于Zend 文件上传和元素装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:47