1

我正在使用ViewScript來裝飾我的表單元素。使用無線電元素,分隔符通常可以被覆蓋,但是當我使用ViewScript時,覆蓋忽略。使用ViewScript裝飾器時覆蓋Zend_Form無線元素上的分隔符

當我使用下面的調用和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> 

回答

7

在視圖腳本,這條線,$這 - > {$這個 - >元素 - >幫助}(...),運行在列出的功能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> 
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'))   
    ) 
)); 
+0

你的解決方案適用於您的情況,但使用視圖腳本時就不會奏效。感謝您發佈它,它可能會派上用場,因爲其他人試圖找到解決方案。 – Sonny 2010-06-01 13:44:58

2

實際上,它可以在一個行完成:

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

這是設置分隔符的方法,如果您使用標準裝飾器,則它可以工作。我有一個忽略設置的ViewScript裝飾器。如果你看看我的解決方案,你會看到我是如何解決問題的。 – Sonny 2013-07-22 18:24:20