2009-04-22 61 views
6

我有,我已经像这样创造了一个按钮元素:Zend Framework Zend_Form装饰器:<span>内部按钮元素?

$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来做到这一点的最好方法?

回答

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中,因为所有可以在元素对象上填充的成员。这就是说,这是一个开始,它解决了你的问题。

+0

谢谢你的详细回复。我知道ViewScript装饰器,但我不知道它可以应用于单个元素。 – leek 2009-05-02 05:05:37

0

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

只需多加一个setAttrib(通过)电话:

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

然后在你的CSS:

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

我加入显示:内联,因为这将使该按钮的行为像一个跨度。这应该让你至少开始。当然,您也可以将CSS放在按钮的id上,只需使用display:inline即可获得span范围行为。

+0

我的目标其实是做“按钮跨度{显示:无;}”隐藏按钮的文本(但我想它仍然在那里人没有CSS/JS支持)。 – leek 2009-04-23 00:09:32

+0

Aaah。我知道还有别的东西! – Kekoa 2009-04-23 00:24:49

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, 
      ) 
)); 
1

只是逃避标签工作正常。只有当某人开始搞乱标签装饰器时(通常在定制标签时发生)

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

对于那些谁仍是一头雾水,这里是一个示例代码样式的形式表,并具有在双跨包裹按钮:

//(...)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' 
      )); 

其中$形式是Zend_Form的元件。希望这可以帮助!

0

尝试

$element->addDecorator('Label', array('tag' => 'span', 'class' => 'aClass'))