2012-03-26 59 views
2

我使用在发现自定义表单装饰:http://code.google.com/p/digitalus-cms/source/browse/trunk/library/Digitalus/Form/Decorator/Composite.php?r=767Zend框架类的自定义表单装饰

在文件(行70)的底部是:

$output = '<div class="form_element">' 
       . $label 
       . $input 
       . $errors 
       . $desc 
       . '</div>'; 

我想使DIV类动态并在我的控制器中创建元素时传递。我使用的任何内置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) 
     )); 

任何想法将非常感激。感谢您花时间看!

回答

2

现在知道为什么所有的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()方法...

+0

+1不错的答案,你打了我一分钟:)我一直在思考'getOptions '和构造函数。 – drew010 2012-03-27 03:20:44

+0

谢谢,通常我只是帮助zf,它在这里更安静:) – 2012-03-27 03:23:41

+0

很棒 - 像魅力一样工作!刚刚定义addPrefixPath,同时初始化我的表单,一切都很好。非常感谢。 – user1199981 2012-03-30 19:08:03