Для этого вы можете создать свой собственный декоратор, например:
class My_Decorator_ElementWrapper extends Zend_Form_Decorator_Abstract
{
public function render($content)
{
$class = 'form-element';
$errors = $this->getElement()->getMessages();
if (!empty($errors))
$errors .= ' has-errors';
return '<div class="'.$class.'">' . $content . '</div>';
}
}
Теперь вы можете просто зарегистрировать этот декоратор для элемента:
$element->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
$element->addDecorator('ElementWrapper');
Вы также можете зарегистрировать префиксный путь для всех элементов одновременно, используя вместо этого $form->addElementPrefixPath()
.
Если вы хотите добавить этот декоратор (и путь префикса) автоматически для всех элементов, я предлагаю вам расширить каждый элемент, соответствующий Zend (например, make My_Form_Element_Text
, который расширяет Zend_Form_Element_Text
), а затем добавить путь префикса в функцию инициализации и override loadDefaultDecorators()
, чтобы добавить ElementWrapper
в конец цепочки декораторов. Например, так loadDefaultDecorators()
ищет Zend_Form_Element_Text
:
public function loadDefaultDecorators()
{
if ($this->loadDefaultDecoratorsIsDisabled()) {
return $this;
}
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('ViewHelper')
->addDecorator('Errors')
->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
->addDecorator('HtmlTag', array('tag' => 'dd',
'id' => $this->getName() . '-element'))
->addDecorator('Label', array('tag' => 'dt'));
}
return $this;
}
Вы просто добавляете ->addDecorator('ElementWrapper')
в конце цепочки. Итак, чтобы показать конкретный пример My_Form_Element_Text
:
class My_Form_Element_Text extends Zend_Form_Element_Text
{
public function init()
{
$this->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
}
public function loadDefaultDecorators()
{
if ($this->loadDefaultDecoratorsIsDisabled()) {
return $this;
}
$decorators = $this->getDecorators();
if (empty($decorators)) {
$this->addDecorator('ViewHelper')
->addDecorator('Errors')
->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
->addDecorator('HtmlTag', array('tag' => 'dd',
'id' => $this->getName() . '-element'))
->addDecorator('Label', array('tag' => 'dt'))
->addDecorator('ElementWrapper');
}
return $this;
}
}
person
reko_t
schedule
06.10.2010