Php Zend框架-Zend_表单装饰器问题
我有一个扩展Zend_表单的类,如下所示(简化): 然后,我使用该类创建所有表单:Php Zend框架-Zend_表单装饰器问题,php,zend-framework,zend-form,Php,Zend Framework,Zend Form,我有一个扩展Zend_表单的类,如下所示(简化): 然后,我使用该类创建所有表单: class ExampleForm extends Core_Form { public function init() { // Example Field $example = new Zend_Form_Element_Hidden('example'); $this->addElement($example); } } 在我的
class ExampleForm extends Core_Form
{
public function init()
{
// Example Field
$example = new Zend_Form_Element_Hidden('example');
$this->addElement($example);
}
}
在我的一个视图中,我只需要显示这一个字段(没有Zend_表单生成的任何其他内容)。因此,在我看来,我有:
<?php echo $this->exampleForm->example; ?>
除了生成如下字段外,它工作正常:
<li><input type="hidden" name="example" value=""></li>
class ExampleForm extends Core_Form
{
public function init()
{
//Example Field
$example = new Zend_Form_Element_Hidden('example');
$this->addElement($example);
}
public function loadDefaultDecorators()
{
$this->example->setDecorators(array('ViewHelper'));
}
}
这显然是因为我将元素装饰器设置为包含HtmlTag:tag=>li
我的问题是:如何禁用此元素的所有装饰器。我不需要隐藏输入元素的装饰器。将表单元素的装饰器重置为仅使用“ViewHelper”。例如:
<?php echo $this->exampleForm->example->setDecorators(array('ViewHelper')) ; ?>
显然,风景不是进行此操作的理想场所,但您可以理解。请注意,调用setDecorator***s***()会重置所有的decorator,而不是添加新的decorator 设置它的最佳位置是公共函数loadDefaultDecorators() 例如:
<li><input type="hidden" name="example" value=""></li>
class ExampleForm extends Core_Form
{
public function init()
{
//Example Field
$example = new Zend_Form_Element_Hidden('example');
$this->addElement($example);
}
public function loadDefaultDecorators()
{
$this->example->setDecorators(array('ViewHelper'));
}
}
如果禁用隐藏元素上的dd/dt装饰器,那么XHTML将无效,因为在dl中会有一些无效项。唯一的解决方案是在所有表单元素上禁用这些装饰器,而不仅仅是隐藏的元素,并在整个表单上禁用它们。为了保持一致性,您需要跨所有表单执行此操作 伊姆霍,在采埃孚,这是一个糟糕的设计决策。我的意思是,说输入的值是“术语”的“定义”在语义上是一个可爱的想法,但它并没有被充分考虑 这里有同样的问题:以下是我的工作:
class M_Form_Element_Hidden extends Zend_Form_Element_Hidden {
public function init() {
$this->setDisableLoadDefaultDecorators(true);
$this->addDecorator('ViewHelper');
$this->removeDecorator('DtDdWrapper');
$this->removeDecorator('HtmlTag');
$this->removeDecorator('Label');
return parent::init();
}
}
然后以你的形式
$element = new M_Form_Element_Hidden('myElement');
$this->addElement($element);
如果要以这种方式添加元素,请执行以下操作:
$this->addElement(
'text',
'a1',
array('required' => true, 'validators' => array('Alpha'))
);
您可以使用以下内容获取每个元素的dd/dt
标记:
$this->setElementDecorators(array('ViewHelper'));
或者,如果要以其他方式添加元素:
$nombre1 = new Zend_Form_Element_Text(
'n1',
array('id'=> 'Nombre1', 'validators' => array('Alpha') )
);
//$nombre1->setDecorators(array('ViewHelper'));
$this->addElement($nombre1);
您需要取消注释:
//$nombre1->setDecorators(array('ViewHelper'));
以禁用dd/dt
标记。
最后一种方法只是禁用当前元素,表单中的其他元素将保持
标记正常。谢谢!我不知道为什么我没有考虑重写loadDefaultDecorators()函数。