2010-02-09 68 views
1

我目前正在尝试构建一个简单的自定义图层,我将扩展而不是Zend_Form。例如,My_Form。Zend_Form覆盖元素默认为自定义布局

我希望我所有的窗体看起来都一样,所以我在My_Form中设置它。这是迄今为止。

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'td')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

而我所有的表格都会扩展这个。现在这个工作正常,问题出现在$ _elementDecorators数组中。我正在将标签包装在“td”中,Label Decorator将默认的“id”应用于该“td”,但我想要为该“td”添加一个类。

无论如何要完成这个,这个数组?如果没有,有没有更好的方法来做到这一点?或者如果是这样,有人可以向我描述这个数组是如何工作的吗?

期望的结果:

<tr> 
    <td class='label_cell'> 
     <label /> 
    </td> 
    <td class='value_cell'> 
     <input /> 
    </td> 
</tr> 

谢谢。

回答

1

我找到了一个解决方案,虽然不知道它是最好的。

在这里,我决定创建一个自定义装饰器并加载它。

/** 
* Overide the default, empty, array of element decorators. 
* This allows us to apply the same look globally 
* 
* @var array 
*/ 
protected $_elementDecorators = array(
    'ViewHelper', 
    'Errors', 
    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
    array('CustomLabel', array('tag' => 'td')), 
    array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
); 

/** 
* Prefix paths to use when creating elements 
* @var array 
*/ 
protected $_elementPrefixPaths = array(
    'decorator' => array('My_Form_Decorator' => 'My/Form/Decorator/') 
); 

装饰:

class My_Form_Decorator_CustomLabel extends Zend_Form_Decorator_Label 
{ 
    public function render($content) 
    { 
     //... 
     /** 
     * Line 48 was added for the cutom class on the <td> that surrounds the label 
     */ 
     if (null !== $tag) { 
      require_once 'Zend/Form/Decorator/HtmlTag.php'; 
      $decorator = new Zend_Form_Decorator_HtmlTag(); 
      $decorator->setOptions(array('tag' => $tag, 
             'id' => $this->getElement()->getName() . '-label', 
             'class' => 'label_cell')); 

      $label = $decorator->render($label); 
     } 
     //... 
    } 
} 

虽然这工作得很好,我仍然好奇,如果有做这样一个简单的方法。

任何想法?

0

快速劈我用相同的问题当处理结束了使用:

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'th')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

的区别是:`阵列( '标签',阵列( '标记'=> '' )),

所以你的“标签”列有TH元素,而你的元素列有TD元素。

然后,您可以根据自己的喜好设计风格。