我正在使用自定义表单装饰器,位于:http://code.google.com/p/digitalus-cms/source/browse/trunk/library/Digitalus/Form/Decorator/Composite.php?r=767
文件底部(第 70 行)是:
$output = '<div class="form_element">'
. $label
. $input
. $errors
. $desc
. '</div>';
我想让 DIV 类动态化并在我在 Controller 中创建元素时传递。我使用的任何内置 ZEND 函数仅修改 LABEL 或 INPUT。这是我的元素创建的示例:
$decorator = new Composite();
$this->addElement('text', 'start', array(
'label' => 'Start Number',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum',
),
'decorators' => array($decorator)
));
任何想法将不胜感激。感谢您花时间查看!
最佳答案
现在知道为什么所有 CSS 类都是硬编码的,如果允许您更改当前的装饰器,只需修复 render() 方法:
class Digitalus_Form_Decorator_Composite
{
/* ... */
public function render($content)
{
$element = $this->getElement();
if (!$element instanceof Zend_Form_Element) {
return $content;
}
if (null === $element->getView()) {
return $content;
}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$label = $this->buildLabel();
$input = $this->buildInput();
$errors = $this->buildErrors();
$desc = $this->buildDescription();
$output = '<div class="'.$this->getOption('class').'">'
. $label
. $input
. $errors
. $desc
. '</div>';
switch ($placement) {
case (self::PREPEND):
return $output . $separator . $content;
case (self::APPEND):
default:
return $content . $separator . $output;
}
}
/* ... */
}
在元素创建期间:
$element->setDecorators(array(
/* ... */
array(array('div'=>'Composite'), array('class' => 'my_class_name'))
/* ... */
)));
如果您不想编辑现有装饰器,只需扩展它并覆盖 render() 方法...
关于php - 自定义表单装饰器中的 Zend 框架类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880723/