向 Zend Forms 添加一些 html

2021-12-29 00:00:00 php zend-framework zend-form

我正在寻找一段简单的代码,可以让我将以下 html 添加到我的 zend 表单中:

Im looking for a simple bit of code that will let me add the following html into my zend form:

<div id="wmd-button-bar" class="wmd-panel"></div>

就是这样,它需要在表单中我的方法"元素之上,仅此而已.对于如此简单的操作,我找不到任何不涉及我学习火箭科学(即 Zend 装饰器)的方法.

Thats it, it needs to be above my 'method' element in the form but thats it. For such a simple action I cant find any methods that don't involve me learning rocket science (i.e Zend Decorators).

推荐答案

目前我能想到的唯一方法是在表单中添加一个虚拟元素并移除除具有您指定属性的 'HtmlTag' 之外的所有装饰器在你的问题中.移除装饰器意味着不会渲染实际元素 - 只会渲染 HtmlTag 装饰器.

The only way I can think of at the moment is to add a dummy element to the form and remove all decorators except an 'HtmlTag' with the attributes you specified in your question. Removing the decorators means that the actual element will not be rendered - only the HtmlTag decorator will be rendered.

所以假设你的表单是 $form:

so assuming your form is $form:

$form->addElement(
    'hidden',
    'dummy',
    array(
        'required' => false,
        'ignore' => true,
        'autoInsertNotEmptyValidator' => false,
        'decorators' => array(
            array(
                'HtmlTag', array(
                    'tag'  => 'div',
                    'id'   => 'wmd-button-bar',
                    'class' => 'wmd-panel'
                )
            )
        )
    )
);
$form->dummy->clearValidators();

请注意,您要防止对元素进行任何验证.这只是一种方式 - 可能还有其他方式.

Note that you want to prevent any validation of the element. This is only one way - there are likely others.

输出:

<div id="wmd-button-bar" class="wmd-panel"></div>

有一个很好的.

There is a good article describing decorators.

相关文章