php - 有没有比使用装饰器更好的设计 zend_forms 的方法?

标签 php zend-framework zend-form zend-decorators

我目前正在使用 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/

相关文章:

php - Jquery/PHP + 创建 MySQL 日期类型

php - 表单未将帖子值绑定(bind)到实体

php - Zend Framework 1.12 选择元素 - 名称数组问题

php - Zend_Db_选择 : Working with JOIN's

php - Zend 框架 : How to read email attachments (and save to disk)?

php - Zend CSRF 哈希代码检查是自动处理的吗?

php - 通过 Annotation Builder 在 Zend Framework 2 Forms 中填充关系数据

php - 如何修复 Class 'Album\Controller\AlbumController' not found 错误?

php - Laravel 获取 500 内部服务器错误

php - 如何通过url向mysql中添加数据