我在使用 Zend_Form 删除默认装饰器集时遇到了麻烦。
我正在尝试扩展 Zend_Form 以实现不同的装饰器风格。
class CRM_Form extends Zend_Form
{
public function init()
{
$this->setDisableLoadDefaultDecorators(true);
$this->addDecorator('FormElements')
->addDecorator('Form');
$this->setElementDecorators(array(
'ViewHelper',
'Label',
'Errors',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'p'))
));
}
}
当我尝试像这样使用这个类时:
$form = new CRM_Form();
$form->setAction('/')->setMethod('post');
$id = $form->createElement('text','id')->setLabel('ID:');
$form->addElement($id);
使用旧的装饰器(定义列表)而不是我的段落样式。
如果我在 CRM_Form 类的 init() 方法中添加元素 (),它们将使用我设置的样式。
如何强制使用该类创建的所有元素都使用我的默认样式?
最佳答案
当您在 init 中调用 setElementDecorators 时,您没有任何要装饰的元素,因此什么也不会发生。相反,您可以覆盖 Zend_Form::createElement 函数,它将执行以下操作:
- 如果选项数组包含装饰器列表,则直接传递而不做任何更改。
- 如果选项没有,则添加默认值。
..
// not tested
public function createElement($type, $name, $options = null)
{
if ( !is_array($options) ) {
$options = array();
}
if ( !isset($options['decorators']) ) {
$options['decorators'] = array(
'ViewHelper','Label','Errors',
new Zend_Form_Decorator_HtmlTag(array('tag' => 'p'))
);
// I'd make this array a protected class member
}
return parent::createElement($type, $name, $options);
}
关于php - Zend_Form 去除默认装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1859431/