1

我想将我的表单包装在一个表格中,因此 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>

我怎样才能调整它,使标签是我想要的方式?

4

1 回答 1

2

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

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

于 2012-11-03T11:44:39.830 回答