我有一个奇怪的问题。Zend Framework自定义表单元素得到重复
我创建了一个自定义表单元素。我用两个select-form-elements创建它来选择时间(00:00)。
奇怪的是,当我在表单中使用它的时候,第二次(因为约会有一个开始和结束时间)第一个被再次显示。所以我最终得到了三次form-element。当我再次使用它时,它也显示了前两个,我最终以6次表单元素结束。
有谁知道如何防止他发生?
验证码:
表单元素
<?php
class App_Form_Element_TimeSelect extends Zend_Form_Element_Xhtml
{
public $helper = 'FormTimeSelect';
protected $hour = null;
protected $minutes = null;
public function setHour($hour)
{
$this->hour = $hour;
return $this;
}
public function setMinutes($minutes)
{
$this->minutes = $minutes;
return $this;
}
public function setValue($value)
{
if (is_array($value)
&& isset($value['hour'])
&& isset($value['minutes']))
{
$this->setHour($value['hour'])
->setMinutes($value['minutes']);
}
}
public function getValue()
{
if (!$this->hour || !$this->minutes)
return false;
return $this->hour . ':' . $this->minutes;
}
}
视图助手
<?php
class App_View_Helper_FormTimeSelect extends Zend_View_Helper_FormElement {
protected $html = '';
public function FormTimeSelect($name, $value = null, $attribs = null)
{
$hour = '00';
$minute = '00';
if ($value)
list($hour, $minute) = split(':', $value);
$helper = new Zend_View_Helper_FormSelect();
$helper->setView($this->view);
$hourarray = array('00' => '00', '01' => '01', '02' => '02', '03' => '03',
'04' => '04', '05' => '05', '06' => '06', '07' => '07', '08' => '08',
'09' => '09', '10' => '10', '11' => '11', '12' => '12', '13' => '13',
'14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18',
'19' => '19', '20' => '20', '21' => '21', '22' => '22', '23' => '23');
$minutearray = array('00' => '00', '15' => '15', '30' => '30', '45' => '45');
$this->html .= $helper->formSelect($name . '[hour]', $hour, null, $hourarray);
$this->html .= ' : ';
$this->html .= $helper->formSelect($name . '[minutes]', $minute, null, $minutearray);
return $this->html;
}
}
谢谢!
Peter
请先尝试不使用数组表示法($ name [hour])。 Zend_Form不太方便,您可能需要使用子表单来使用它们。 – 2011-03-13 10:30:23