Zend Form 不会呈现
我有一个简单的 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'))
));
表单就会消失。我的视图只有 form; ?>
如果我删除 setElementDecorators,表单将正确显示(当然没有表格布局)。
I have a simple zend form
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'))
));
}
}
trying to create a table based form. But as soon as I add the element decorators
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
form disappears. My view only has <?php echo $this->form; ?>
and if I remove the setElementDecorators, form displays correctly (without table layout of course).
And I followed this
Tutorial - Table layout with Zend Framework form decorators
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您没有显示警告/异常,并且您从
Zend_Form_Element_File
元素中收到异常。您可以为所有元素(包括这些文件元素)设置装饰器。但是文件元素需要文件装饰器才能工作。在
setElementDecorators
之后设置文件元素的装饰器,看看结果如何。或者只是忽略文件元素以测试这是否是导致问题的原因。My guess is that you don't show your warnings/exceptions and you're getting an exception from the
Zend_Form_Element_File
elements. You set the decorators for all the elements including those file elements. But file elements need the file decorator in order for them to work.Set the decorators for the file elements after the
setElementDecorators
and see how that turns out. Or just leave out the file elements in order to test if that's what's causing your problem.