Zend框架表格装饰器

时间:2012-11-02 18:20:56

标签: zend-framework zend-form

我想把表单包装在一个表中,所以html输出看起来像:

<form enctype="application/x-www-form-urlencoded" method="post" action="">
    <table width="100%" cellspacing="0" cellpadding="0">
        <tr>
            <td id="name-label" class="td2 required">Page Name</td>
            <td class="td3">
                <input type="text" name="name" id="name" value="">
            </td>
        </tr>
        <tr>
            <td id="seo_name-label" class="td2 required">Page SEO Name</td>
            <td class="td3">
                <input type="text" name="seo_name" id="seo_name" value="">
            </td>
        </tr>
        <tr>
            <td id="title-label" class="td2 optional">Page Title</td>
            <td class="td3">
                <input type="text" name="title" id="title" value="">
            </td>
        </tr>
        <tr>
            <td id="order-label" class="td2 required">Page SEO Name</label></td>
            <td class="td3">
                <input type="text" name="order" id="order" value="">
            </td>
        </tr>
        <tr>
            <td class="td1" colspan="2" align="center">
                <input type="submit" name="Save" id="Save" value="Save">
            </td>
        </tr>
        <input type="hidden" name="csrf" value="0fd12c3fd912bb8f2e59ccae28c886f5" id="csrf">
    </table>
</form>

所以我基于一些搜索写了这个:

class Forms_PageEdit extends Zend_Form
{
    public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(
            array('data' => 'HtmlTag'), 
            array('tag' => 'td', 'class' => 'td3')
        ),
        array('Label', 
            array('tag' => 'td', 'class'=>'td2')
        ),
        array(
            array('row' => 'HtmlTag'), 
            array('tag' => 'tr')
        ),
    );

    public function init()
    {
        $this->setMethod('post');
        $this->setDisableLoadDefaultDecorators(true);
        $this->removeDecorator('DtDdWrapper');
        $this->setDecorators(array(
                    'FormElements',
                    array('HtmlTag',array('tag'=>'table', 'width'=>'100%', 'cellspacing' => '0', 'cellpadding'=>'0')),
                    'Form'
                ));

        // Add an page name
        $this->addElement('text', 'name', array(
            'label'      => STC_Model::getInstance('AdminPhrases')->getPhraseByName('pages_edit_name',3),
            'decorators' => $this->elementDecorators,
            'required'   => true,
            'filters'    => array('StringTrim'),
        ));

        // add Page SEO Name Element
        $this->addElement('text', 'seo_name', array(
            'label'      => STC_Model::getInstance('AdminPhrases')->getPhraseByName('pages_edit_seo',3),
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        // add Page Title Element
        $this->addElement('text', 'title', array(
            'label'      => STC_Model::getInstance('AdminPhrases')->getPhraseByName('pages_edit_title',3),
            'decorators' => $this->elementDecorators,
        ));

        // add Page Title Element
        $this->addElement('text', 'order', array(
            'label'      => STC_Model::getInstance('AdminPhrases')->getPhraseByName('pages_edit_seo',3),
            'decorators' => $this->elementDecorators,
            'required'   => true,
            'validators' => array(                                 
                                array('Digits'),
                                array('GreaterThan', -1),
                            ),
        ));


        // Add the submit button
        $this->addElement('submit', STC_Model::getInstance('AdminPhrases')->getPhraseByName('save'), array(
            'decorators' => array(
                    'ViewHelper',
                    'Errors',
                    array(
                        array('data' => 'HtmlTag'), 
                        array('tag' => 'td', 'class' => 'td1', 'colspan' => '2', 'align' => 'center')
                    ),
                    array(
                        array('row' => 'HtmlTag'), 
                        array('tag' => 'tr')
                    ),
                ),
            'ignore'   => true,
        ));
        $element = $this->getElement(STC_Model::getInstance('AdminPhrases')->getPhraseByName('save'));
        $element->removeDecorator('label');
        $element->removeDecorator('DtDdWrapper');

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));

        $element = $this->getElement('csrf');
        $element->removeDecorator('label');
        $element->removeDecorator('DtDdWrapper');
    }
}

但它真正为每个标签做的是:

<td id="name-label"><label for="name" class="td2 required">Page Name</label></td>

如何调整它以便标签符合我的要求?

1 个答案:

答案 0 :(得分:2)

您可能会发现viewScript装饰器正是您要找的。

此装饰器允许您将Zend_Form元素放入您描述的HTML中。