【问题标题】:Zend: How do i create custom form elements using setDecorators()?Zend:如何使用 setDecorators() 创建自定义表单元素?
【发布时间】:2013-02-15 14:28:42
【问题描述】:

我需要如下的html表单:

<form>
<div class="es_form_txt">Name</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div>
<div class="es_form_txt">Gender</div>
<div class="es_form">
    <input type="radio" value="" name=""> Male
    <input type="radio" value="" name=""> Female
    <input type="radio" value="" name=""> Other
</div>
<div class="clear1"></div>
<div class="es_form_txt">Country Code</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div>
<div class="es_form_txt">Phone</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div> 
<div class="clear1"></div>
<div class="es_form">&nbsp;</div>
<div class="es_form"><input type="button" class="button" value="Update"></div>
<div class="clear"></div>
</form>

我在这里尝试了各种解决方案都不起作用!谁能指出我正确的方向?

【问题讨论】:

    标签: zend-framework zend-form decorator


    【解决方案1】:

    我发现当我需要在使用Zend_Form时显示特定的HTML时,最可靠的解决方案通常是使用ViewScript decorator。

    使用 Zend_Form 构建一个普通表单。

    <?php
    //application/forms/Search.php
    class Application_Form_Search extends Zend_Form
    {
        public function init()
        {
            $this->setMethod('POST');
            //assign the view script to the decorator, this can also be done in the controller
            $this->setDecorators(array(
                array('ViewScript', array(
                        'viewScript' => '_searchForm.phtml'
                ))
            ));
            // create new element
            $query = $this->createElement('text', 'query');
            // element options
            $query->setLabel('Search Keywords');
            // add the element to the form
            $this->addElement($query);
            //submit element
            $submit = $this->createElement('submit', 'search');
            $submit->setLabel('Search Site');
            $submit->setDecorators(array('ViewHelper'));
            $this->addElement($submit);
        }
    }
    

    然后构建用于呈现表单的实际脚本。

    <!-- ~/views/scripts/_searchForm.phtml -->
    <article class="search">
        <!-- set the action and the method -->
        <form action="<?php echo $this->element->getAction()?>" method="<?php echo $this->element->getMethod()?>">
            <table>
                <tr>
                    <!-- you can render individual decorators. -->
                    <th><?php echo $this->element->query->renderLabel()?></th>
                </tr>
                <tr>
                    <td><?php echo $this->element->query->renderViewHelper()?></td>
                </tr>
                <tr>
                    <!-- or you can render a complete element -->
                    <td><?php echo $this->element->search?></td>
                </tr>
            </table>
        </form>
    </article>
    

    目前呈现为:

    <article class="search">
        <form action="/video/index/display" method="post">
            <table>
                <tr>
                    <th>
                        <dt id="query-label">
                            <label for="query" class="optional">Search Keywords</label>
                        </dt>
                    </th>
                </tr>
                <tr>
                    <td>
                        <input type="text" name="query" id="query" value="" placeholder="Movie Title" size="20">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" name="search" id="search" value="Search Video Collection!">
                    </td>
                </tr>
            </table>
        </form>
    </article>   
    

    需要进行一些实验才能准确获得所需的输出。

    【讨论】:

    • 这很好,但我想知道在我的视图文件中包含 php 代码是否是一种好习惯——我真的不喜欢它。无论如何,您的解决方案是一种快速的解决方案:)
    • 在视图文件中没有办法避免php,这就是为什么它们是.phtml(php html)。如果您愿意,您可以使用单独的模板系统,但这可能需要付出很多努力而获得很少的回报。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多