【问题标题】:Zend Form won't renderZend Form 不会渲染
【发布时间】:2012-01-11 15:07:02
【问题描述】:

我有一个简单的 zend 表单

class Form_Upload extends Zend_Form
{
    private $_networks = array();
    private $_reportYear;
    public function __construct($options)
    {
        if (array_key_exists('networks', $options)) {
            $this->_networks = $options['networks'];
            unset($options['networks']);
        }
        if (array_key_exists('reportYear', $options)) {
            $this->_reportYear = $options['reportYear'];
            unset($options['reportYear']);
        }
        parent::__construct($options);
    }
    public function init()
    {
        $this->setMethod(Zend_Form::METHOD_POST);
        $this->setAttrib('enctype', 'multipart/form-data');

        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form'
        ));

        // Report year
        $reportYear = new Zend_Form_Element_Hidden('ReportYear');
        $reportYear->setValue($this->_reportYear);
        $this->addElement($reportYear);

        // Station File
        $stationFile = new Zend_Form_Element_File('StationFile');
        $stationFile->setLabel('Station File')
            ->setMaxFileSize(102400)
            ->addValidator('Extension', false, 'csv')
            ->setValueDisabled(true);
        $this->addElement($stationFile);

        $stationFileNetwork = new Zend_Form_Element_Select('StationFileNetwork');
        $stationFileNetwork->setLabel('Network')
                           ->addMultiOptions($this->_networks);
        $this->addElement($stationFileNetwork);

        $stationFileComment = new Zend_Form_Element_Textarea('StationFileComment');
        $stationFileComment->setLabel('Comments')
                           ->setAttrib('cols', 30)
                           ->setAttrib('rows', 5);
        $this->addElement($stationFileComment);

        // Configuration File
        $configurationFile = new Zend_Form_Element_File('ConfigurationFile');
        $configurationFile->setLabel('Configuration File')
            ->setMaxFileSize(102400)
            ->addValidator('Extension', false, 'csv')
            ->setValueDisabled(true);
        $this->addElement($configurationFile);

        $configurationFileNetwork = new Zend_Form_Element_Select('ConfigurationFileNetwork');
        $configurationFileNetwork->setLabel('Network')
            ->addMultiOptions($this->_networks);
        $this->addElement($configurationFileNetwork);

        $configurationFileComment = new Zend_Form_Element_Textarea('ConfigurationFileComment');
        $configurationFileComment->setLabel('Comments')
            ->setAttrib('cols', 30)
            ->setAttrib('rows', 5);
        $this->addElement($configurationFileComment);

        // Measurement File
        $measurementFile = new Zend_Form_Element_File('MeasurementFile');
        $measurementFile->setLabel('Measurement File')
            ->setMaxFileSize(102400)
            ->addValidator('Extension', false, 'csv')
            ->setValueDisabled(true);
        $this->addElement($measurementFile);

        $measurementFileNetwork = new Zend_Form_Element_Select('MeasurementFileNetwork');
        $measurementFileNetwork->setLabel('Network')
            ->addMultiOptions($this->_networks);
        $this->addElement($measurementFileNetwork);

        $measurementFileComment = new Zend_Form_Element_Textarea('MeasurementFileComment');
        $measurementFileComment->setLabel('Comments')
            ->setAttrib('cols', 30)
            ->setAttrib('rows', 5);
        $this->addElement($measurementFileComment);

        // Submit
        $submit = new Zend_Form_Element_Submit('Upload');
        $submit->setLabel('Upload');
        $this->addElement($submit);

    $this->setElementDecorators(array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
    ));

    }
}

尝试创建基于表格的表单。但是一旦我添加了元素装饰器

$this->setElementDecorators(array(
    'ViewHelper',
    'Errors',
    array(array('data' => 'HtmlTag'), array('tag' => 'td')),
    array('Label', array('tag' => 'td')),
    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));

表格消失。我的视图只有<?php echo $this->form; ?>,如果我删除 setElementDecorators,表单会正确显示(当然没有表格布局)。

我跟着这个 Tutorial - Table layout with Zend Framework form decorators

【问题讨论】:

  • 您是否在控制器脚本中的某处分配了 $form 变量?类似于这个 $this->view->form = $form; ?
  • 嗯,当然 :) 否则当我删除 setElementDecorator 时它不会起作用。
  • 对不起 - 辛苦了一天 ;)

标签: zend-framework zend-form


【解决方案1】:

我的猜测是您没有显示警告/异常,并且您从 Zend_Form_Element_File 元素中获得了异常。您为所有元素设置装饰器,包括那些文件元素。但是文件元素需要文件装饰器才能工作。

setElementDecorators 之后为文件元素设置装饰器,看看结果如何。或者只是省略文件元素以测试是否是导致您的问题的原因。

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2011-08-21
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多