【问题标题】:Zend File Upload and Element DecoratorsZend 文件上传和元素装饰器
【发布时间】:2011-11-26 16:58:07
【问题描述】:

我的问题是,以下 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 ) 

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

有人可以帮忙吗?

TIA 马特

【问题讨论】:

    标签: zend-framework zend-form zend-file


    【解决方案1】:

    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。

    【讨论】:

    • 感谢 4 您的帮助。添加此引发:警告:表单捕获的异常:方法 getMaxFileSize 不存在堆栈跟踪:#0
    • 行得通。在zend的文档中没有注意到这一点!? :/谢谢!
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2012-09-09
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多