更新我能够通过创建自定义标签装饰器来实现这一点,该装饰器扩展了Zend / Form / Decorator / Label.php。我向它添加了一个setTagClass()方法并覆盖了render方法,以创建带有所需类的封闭标记。可能有一种更优雅的方式,但这似乎有效。
我正在寻找有关如何使用装饰器在标签的dt元素上设置类的信息。下面的第三行代码在标签上设置类,并将标签包装在dt标记中。我想知道如何在dt标签上设置类。
$txtLangPrefOther = $this->createElement('text','langPrefOther');
$txtLangPrefOther->setLabel('Language Preference Other:'));
$txtLangPrefOther->getDecorator('Label')->setOptions(array('tag' => 'dt', 'class' => 'other'));
这会产生
等输出<dt id="langPrefOther-label">
<label for="langPrefOther" class="other">Language Preference Other:</label>
</dt>
<dd id="langPrefOther-element">
<input type="text" id="langPrefOther" name="langPrefOther" ">
</dd>
我希望它看起来像
<dt id="langPrefOther-label" class="other">
<label for="langPrefOther">Language Preference Other:</label>
</dt>
<dd id="langPrefOther-element">
<input type="text" id="langPrefOther" name="langPrefOther" ">
</dd>
答案 0 :(得分:5)
Label
装饰器的属性名为tagClass
!
试试这个:
$element->addDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'dd', 'class' => $class )),
array('Label', array('tag' => 'dt', 'class' => $class, 'tagClass' => $class))
));
答案 1 :(得分:4)
因为它是一个表单装饰器而不是元素装饰器。试试这个:
$this->setDecorators(
array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'theclass')),
'Form'
));
答案 2 :(得分:0)
这可能是一种更简洁的方法,但这是我的自定义装饰器来实现这一点(注意:你需要扩展Zend_Form_Decorator_Label类):
/**
* Class for HTML tag surrounding label
* @var string
*/
protected $_tagClass;
/**
* Set HTML tag's class
*
* @param string $tag
* @return Zend_Form_Decorator_Label
*/
public function setTagClass($tagClass)
{
if (empty($tagClass)) {
$this->_tagClass = null;
} else {
$this->_tagClass = (string) $tagClass;
}
$this->removeOption('tagClass');
return $this;
}
/**
* Get HTML tag's class, if any
*
* @return void
*/
public function getTagClass()
{
if (null === $this->_tagClass) {
$tagClass = $this->getOption('tagClass');
if (null !== $tagClass) {
$this->removeOption('tagClass');
$this->setTagClass($tagClass);
}
return $tagClass;
}
return $this->_tagClass;
}
/**
* Render a label
*
* @param string $content
* @return string
*/
public function render($content)
{
$element = $this->getElement();
$view = $element->getView();
if (null === $view) {
return $content;
}
$label = $this->getLabel();
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$tag = $this->getTag();
$tagClass = $this->getTagClass();
$id = $this->getId();
$class = $this->getClass();
$options = $this->getOptions();
if (empty($label) && empty($tag)) {
return $content;
}
if (!empty($label)) {
$options['class'] = $class;
$label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
} else {
$label = ' ';
}
if (null !== $tag) {
require_once 'Zend/Form/Decorator/HtmlTag.php';
$decorator = new Zend_Form_Decorator_HtmlTag();
$decorator->setOptions(array('tag' => $tag,
'id' => $id . '-label',
'class' => $tagClass));
$label = $decorator->render($label);
}
switch ($placement) {
case self::APPEND:
return $content . $separator . $label;
case self::PREPEND:
return $label . $separator . $content;
}
}