6

我有一个这样创建的按钮元素:

$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('My Button');
$submit->setDecorators(array(
    'ViewHelper',
     array('HtmlTag', array('tag' => 'li'))
));
$submit->setAttrib('type', 'submit');

这会生成以下 HTML:

<li>
    <label for="submit" class="optional">My Button</label> 
    <button name="submit" id="submit" type="submit">My Button</button>
</li>

我想用 <span> 包裹按钮的内部,如下所示:

<button...><span>My Button</span></button>

使用 Zend_Form 执行此操作的最佳方法是什么?

4

5 回答 5

7

我已经尝试过,但最终未能使用相同的方法自己实现这一目标。似乎最简单的方法是这样做:

...
$submit->setLabel('<span>My Button</span>');
...

但是,跨度将被转义。完全可以关闭标签装饰器的转义,但是,添加标签装饰器会错误地呈现输出,例如:

$decorator = array(
    array('ViewHelper'),
    array('HtmlTag', array('tag' => 'li')),
    array('Label', array('escape' => false))
);

$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('<span>My Button</span>');
$submit->setDecorators($decorator);
$submit->setAttrib('type', 'submit');

...呈现:

<label for="submit" class="optional"><span>My Button</span></label>
<li>
    <button name="submit" id="submit" type="submit">&lt;span&gt;My Button&lt;/span&gt</button>
</li>

...除了在语义上不正确(易于修复)之外,它仍在转义元素内的 span 标签。

所以你会怎么做?

好吧,我认为最好的方法(当涉及到对 Zend_Form 渲染的严格控制时,这是我的元建议)是使用ViewScript装饰器。

$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('My Button');
$submit->setDecorators(array(array('ViewScript', array('viewScript' => '_submitButton.phtml'))));
$submit->setAttrib('type', 'submit');

...然后在_submitButton.phtml中定义以下内容:

<li>
    <?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
    <button 
    <?php 

    $attribs = $this->element->getAttribs();

    echo
    ' name="' . $this->escape($this->element->getName()) . '"' .
    ' id="' . $this->escape($this->element->getId()) . '"' . 
    ' type="' . $this->escape($attribs['type']) . '"';
    ?>
    <?php

    $value = $this->element->getValue();

    if(!empty($value))
    {
        echo ' value="' . $this->escape($this->element->getValue()) . '"';
    }
    ?>
    >
    <span>
    <?= $this->escape($this->element->getLabel()); ?>
    </span>
    </button>
</li>

_submitButton.phtml文件需要位于视图脚本目录中(您最好使用 为您的表单装饰器添加一个特定的目录$view->addScriptPath('/path/to/my/form/decorators')

这应该呈现您正在寻找的内容。由于我在工作中遇到的灵活性问题,我才刚刚开始研究 ViewScript 装饰器。您会注意到我的脚本不是那么灵活,而且肯定不在 BNF 中,因为元素对象上可以填充所有成员。也就是说,这是一个开始,它可以解决您的问题。

于 2009-05-01T13:58:23.170 回答
5

你可以这样做:

$this->addElement(new Zend_Form_Element_Button(
            'send',
            array(
                'label' => '<span>registrieren</span>',
                'class' => 'button-red',
                'type' => 'submit',
                'escape' => false,
                'required' => false,
                'ignore' => false,
            )
 ));
于 2010-03-14T19:21:17.590 回答
1

只是逃避标签工作正常。只有当有人开始弄乱标签装饰器时它才会中断(通常在自定义标签时发生)

您可以使用 Zend_Form 函数setElementDecorators()并从样式中排除一些元素。阅读Zend_Form 手册中的示例 #3 为某些元素设置装饰器(包括注释!)以获取更多详细信息。

对于那些仍然感到困惑的人,这里有一个示例代码,可以将表单设置为表格,并有一个包含在双跨度中的按钮:

//(...)putting some other elements in the form first

//now add submit button 
$form       ->addElement('button', 'submit', array(
                        'label'     => '<span><span>SUBMIT</span></span>', 
                        'value'     => 'submit'
            ));

$form
            //set decorators for non-button elements
            ->setElementDecorators(array(
                    'ViewHelper',
                    'Errors',
                    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
                    array('Label', array('tag' => 'td')),
                    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), false)
            //and then for button elements only
            ->setElementDecorators(array(
                    'ViewHelper',
                    'Errors',
                    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
                    array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), true)
            //and finally for the form
            ->setDecorators(array(
                    'FormElements',
                    array('HtmlTag', array('tag' => 'table')),
                    'Form'
            ));

其中$form是 Zend_Form 元素。希望这可以帮助!

于 2010-07-06T07:30:21.697 回答
0

我会为按钮添加一个类,然后在你的 CSS 中定义该类以使其像一个跨度。我敢肯定,你想做的不仅仅是坚持下去。

只需再添加一个 setAttrib() 调用:

$submit->setAttrib('class', 'submitButton');

然后在你的 CSS 中:

.submitButton {
   display:inline;
   font-weight:bold; /* Or whatever you're wanting to do */
}

我添加了 display:inline 因为这将使按钮像一个跨度。这至少应该让你开始。当然,您也可以将 CSS 建立在按钮的 id 上,只需使用 display:inline 即可获得跨度行为。

于 2009-04-23T00:03:20.433 回答
0

尝试

$element->addDecorator('Label', array('tag' => 'span', 'class' => 'aClass'))
于 2013-02-23T11:28:51.480 回答