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

Son*_*nny 1 zend-framework zend-form zend-decorators zend-view zend-form-element

我正在使用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(),
));
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Son*_*nny 7

在视图脚本中,这一行$ this - > {$ this-> element-> helper}(...)运行Zend View Helpers文档中列出的函数.这种情况下的函数是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>
Run Code Online (Sandbox Code Playgroud)


gaw*_*ron 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'))          
    )
));
Run Code Online (Sandbox Code Playgroud)