php - Zend_Form 去除默认装饰器

标签 php zend-framework zend-form

我在使用 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 函数,它将执行以下操作:

  1. 如果选项数组包含装饰器列表,则直接传递而不做任何更改。
  2. 如果选项没有,则添加默认值。

..

// 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/

相关文章:

php - 如何在 yii 框架中显示数据库中新添加的列?

php - 使用 mysqli 和 php 实例化构造函数类

php - 如何建模与不同类型对象的关系

zend-framework - Zend Framework - 模块中的模块?

zend-framework - 如何手动设置 Zend_Form 中 dd 元素的 id?

php - 关于 zend 框架的提交问题

php - Symfony 的缓存和权限错误

mysql - Zend 分页器运行速度非常慢 1.12

php - Zend 静态助手

zend-form - 将 inputFilter 附加到动态创建的字段元素