我目前正在使用 zend_decorators 向我的表单添加样式。我想知道是否有其他方法可以做到这一点?写装饰器有点困难。我喜欢使用 div 和 css 风格的休闲风格:
<input type="submit" class="colorfulButton" >
这比给某个控件设置一个装饰器然后添加要简单得多。因为它需要为每个样式实现创建一个装饰器并将其与控件相加。会查看帮助者的技巧吗?
最佳答案
有几种方法。您可以滚动自己的元素 View 助手(我猜这很快就会变得相当笨拙)。
或者...您可以使用表单的 View 脚本,如下所示(非常基本的示例):
class Your_Form extends Zend_Form
{
public function init()
{
$this->setDecorators( array(
'PrepareElements',
array( 'ViewScript', array( 'viewScript' => 'path/to/viewscript.phtml' ) )
) );
// only use basic decorators for elements
$decorators = array(
'ViewHelper',
'Label',
'Errors'
);
// create some element
$someElement = new Zend_Form_Element_Text( 'someElement' );
// set the basic decorators for this element and set a css class
$someElement->setDecorators( $decorators )
->setAttrib( 'class', 'someCssClass' );
// add (potentially multiple) elements to this from
$this->addElements( array(
$someElement
) );
}
}
请参阅standard decorators section about PrepareElements为什么在使用 ViewScript 装饰器时需要为表单设置 PrepareElements 装饰器。
然后在 View 脚本中:
<?
// the form is available to the viewscript as $this->element
$form = $this->element;
?>
<!-- put whatever html you like in this script and render the basic element decorators seperately -->
<div>
<? if( $form->someElement->hasErrors() ): ?>
<?= $form->someElement->renderErrors() ?>
<? endif; ?>
<?= $form->someElement->renderLabel(); ?>
<?= $form->someElement->renderViewHelper(); ?>
</div>
关于php - 有没有比使用装饰器更好的设计 zend_forms 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619593/