dt3674 2010-04-12 02:40
浏览 51
已采纳

有没有更好的方法来设计zend_forms而不是使用装饰器?

I am currently using zend_decorators to add styles to my form. I was wondering if there is an alternative way of doing it? It is a bit difficult to write decorators. I would love the casual one using divs and css style :

<input type="submit" class="colorfulButton" > 

It is much simpler rather than set a decorator for a certain control and add it. Since it requires creating a decorator for each style implementation and adding it up with the control. Will view helpers to the trick?

  • 写回答

2条回答 默认 最新

  • dongxinjun3944 2010-04-14 03:03
    关注

    There's a few ways. You can roll your own element view helpers (which could get rather clumsy soon I guess).

    Or... you could use a viewscript for the form, like this (very basic example):

    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
            ) );
    
        }
    }
    

    See the standard decorators section about PrepareElements for why it's needed to have the PrepareElements decorator set for the form, when using the ViewScript decorator.

    Then in the viewscript:

    <?
        // 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。