Override Separator on Zend_Form Radio Elements When Using a ViewScript Decorator

安稳与你 提交于 2019-12-21 23:57:25

问题


I am using ViewScripts to decorate my form elements. With radio elements, the separator can normally be overridden, but the override is being ignored when I use the ViewScript.

When I use the below call and ViewScript, the radio elements are separated by a '<br />' rather than the space I've specified. If leave the default decorators, the override works.

$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>

回答1:


In the view script, this line, $this->{$this->element->helper}(...), runs the functions listed in the Zend View Helpers documentation. The function in this case is formRadio(). There is a fifth parameter to formRadio() which is the separator! Adding the fifth parameter, by referencing the element, solves the problem:

<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>



回答2:


I've had this problem, I've solved by using setting disableLoadDefaultDecorators to true and separator to &nbsp; or what ever you need.

$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'))          
    )
));



回答3:


Actually, it can be done in one line:

echo $this->element->setSeparator('&nbsp;');


来源:https://stackoverflow.com/questions/2148510/override-separator-on-zend-form-radio-elements-when-using-a-viewscript-decorator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!