2012-01-11 60 views
2

我有一个简单的Zend形式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

+0

您是否在控制器脚本中的某个地方设置$ form变量?类似于这个$ this-> view-> form = $ form; ? – mychiara 2012-01-11 15:09:08

+1

好吧,当然:)否则当我删除setElementDecorator时它不会工作。 – Optimus 2012-01-11 15:12:27

+0

对不起 - 这是漫长的一天;) – mychiara 2012-01-11 18:35:02

回答

1

我的猜测是,你不显示您的警告/异常,你得到从Zend_Form_Element_File元素异常。您为所有元素(包括这些文件元素)设置装饰器。但文件元素需要文件修饰器才能工作。

setElementDecorators后面设置文件元素的装饰器,看看结果如何。或者只是忽略文件元素,以测试这是什么导致你的问题。

+0

当然!非常感谢你! – Optimus 2012-01-11 16:01:25