【问题标题】:Using Zend Form decorators to make table-less form使用 Zend Form 装饰器制作无表表单
【发布时间】:2011-10-11 14:51:59
【问题描述】:

如果我有以下简单的形式:

<?php

class Application_Form_Contact extends Zend_Form
{
    public function init()
    {
        //set the method for the display form to POST
        $this->setMethod('post');

        //create and add e-mail element
        $email = $this    -> createElement('text', 'username')
                              -> setLabel('E-mail:')
                          -> setRequired(true)
                          -> addFilters(array('StringTrim', 'StringToLower'))
                          -> addValidator('EmailAddress')
                          -> addErrorMessage('U dient een geldig e-mail adres in te vullen.');               
        $this->addElement($email);

        //create and add message element
        $message = $this    -> createElement('textarea', 'message')
                            -> setLabel('Bericht:')
                            -> setRequired(true)
                            -> addValidator('StringLength', array(0, 5000))
                            -> addErrorMessage('U dient een bericht in te vullen van maximaal 5000 tekens.');
        $this->addElement($message);

        //submit button
        $this->addElement('submit', 'submit', array(
            'ignore'    => true,
            'label'     => 'Verstuur Bericht'
        ));
    }
}

?>

默认情况下,这将导致表格布局如下:

    <form enctype="application/x-www-form-urlencoded" method="post" action="/contact"><dl class="zend_form"> 
<dt id="username-label"><label for="username" class="required">E-mail:</label></dt> 
<dd id="username-element"> 
<input type="text" name="username" id="username" value=""></dd> 
<dt id="message-label"><label for="message" class="required">Bericht:</label></dt> 
<dd id="message-element"> 
<textarea name="message" id="message" rows="24" cols="80"></textarea></dd> 
<dt id="submit-label">&#160;</dt><dd id="submit-element"> 
<input type="submit" name="submit" id="submit" value="Verstuur Bericht"></dd> 
</dl></form>

这是完全没有必要的,因为一些简单的 HTML 会产生更符合我喜好的表单(因为文本区域在标签下方,而电子邮件输入在标签旁边):

<form enctype="application/x-www-form-urlencoded" method="post" action="/contact">

    <p>
        <label id="username-label" for="username" class="required">E-mail:</label>
        <input type="text" name="username" id="username" value="" />
    </p>

    <br />  

    <p>
        <label id="message-label" for="message" class="required">Bericht:</label>
        <br />
        <textarea name="message" id="message" rows="24" cols="80"></textarea>
    </p>

    <input type="submit" name="submit" id="submit" value="Verstuur Bericht" />

</form>

看起来像这样:http://jsfiddle.net/qmKYa/ 而不是 http://jsfiddle.net/rudhA/2/。

但是我如何让 zend 根本不渲染任何表格,将前两个元素包装在段落标签中并添加两个新行以获得适当的间距?

【问题讨论】:

    标签: php forms zend-framework decorator


    【解决方案1】:

    首先,Zend_Form 使用装饰器来编写表单。您可以更改现有的默认装饰器,也可以编写和添加您自己的装饰器。查看Zend Reference Guide 获取示例。

    关于标签的位置,你可以告诉现有的装饰器对表单元素进行追加或准备。

    以下是一些关于如何操作现有装饰器的示例。

    $element->removeDecorator('DtDdWrapper');
    
    $descriptionDecorator = new Zend_Form_Decorator_Description(array('tag'=>'p','class' => 'description_submit'));
    $descriptionDecorator->setOption('placement','PREPEND');
    $element->addDecorator($descriptionDecorator);
    

    还有一个现有的 Zend_Form_Decorator_Label 类,但我没有为此准备好的示例。

    【讨论】:

    • 谢谢。考虑到 HTML 使用起来非常简单,这个装饰器系统似乎非常复杂。我想我将不得不经历一段看似陡峭的学习曲线。
    • @Tom 如果您只创建一两个表单,那肯定是正确的。如果您创建数十个包含大量元素甚至多页表单的表单,那么这将为您节省大量时间。更不用说过滤器和验证器了
    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 2011-01-10
    • 2012-10-23
    • 2012-04-18
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多