6

我的问题是,以下 Zend Form 会引发错误。问题是“文件”元素和使用 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 ) 

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

有人可以帮忙吗?

TIA 马特

4

1 回答 1

10

File 元素需要它自己的装饰器 - 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'))
    )
);

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

于 2011-09-28T09:23:40.020 回答