使用ViewScript装饰器时覆盖Zend_Form无线电元素上的分隔符

时间:2010-01-27 16:22:47

标签: zend-framework zend-form zend-view zend-form-element zend-decorators

我使用ViewScripts来装饰我的表单元素。使用无线电元素时,通常可以覆盖分隔符,但在使用ViewScript时会忽略覆盖。

当我使用以下调用和ViewScript时,无线电元素由'< br />'分隔而不是我指定的空间。如果保留默认装饰器,则覆盖工作。

$this->addElement('radio', 'active', array(
    'disableLoadDefaultDecorators' => true,
    'decorators' => array(array('ViewScript', array('viewScript' => 'form/multi.phtml'))),
    'label' => 'Active',
    'required' => true,
    'multiOptions' => array('1' => 'Yes', '0' => 'No',),
    'value' => '1',
    'separator' => ' ',
    'filters' => array(),
    'validators' => array(),
));

ViewScript:

<div class="field <?php echo strtolower(end(explode('_',$this->element->getType()))) ?><?php if($this->element->hasErrors()): ?> errors<?php endif; ?>" id="field_<?php echo $this->element->getId(); ?>">
    <?php if (0 < strlen($this->element->getLabel())): ?>
        <?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
    <?php endif; ?>
    <span class="value"><?php echo $this->{$this->element->helper}(
        $this->element->getName(),
        $this->element->getValue(),
        $this->element->getAttribs(),
        $this->element->getMultiOptions()
    ); ?></span>
    <?php if ($this->element->hasErrors()): ?>
        <?php echo $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
    <?php if (0 < strlen($this->element->getDescription())): ?>
        <span class="hint"><?php echo $this->element->getDescription(); ?></span>
    <?php endif; ?>
</div>

3 个答案:

答案 0 :(得分:7)

在视图脚本中,此行$ this-&gt; {$ this-&gt; element-&gt; helper}(...)运行Zend View Helpers documentation中列出的函数。这种情况下的函数是formRadio()。 formRadio()有第五个参数,它是分隔符!通过引用元素添加第五个参数可以解决问题:

<span class="value"><?php echo $this->{$this->element->helper}(
    $this->element->getName(),
    $this->element->getValue(),
    $this->element->getAttribs(),
    $this->element->getMultiOptions(),
    $this->element->getSeparator()
); ?></span>

答案 1 :(得分:6)

我遇到了这个问题,我已经通过将disableLoadDefaultDecorators设置为true并将分隔符设置为&nbsp;或者您需要的任何内容来解决。

$form->addElement('multiCheckbox', 'myFields', array(
    'disableLoadDefaultDecorators' => true
    ,'separator'    => '&nbsp;'
    ,'multiOptions' => array(
        'title'       => 'Title'
        ,'first_name' => 'First Name'
        ,'surname'    => 'Surname'
    )
    ,'decorators'   => array(
        'ViewHelper'
        ,'Errors'
        ,array('HtmlTag', array('tag' => 'p'))          
    )
));

答案 2 :(得分:2)

实际上,它可以在一行中完成:

echo $this->element->setSeparator('&nbsp;');
相关问题