2015-12-09 51 views
0

我想自定義的Zend框架單選按鈕的HTML輸出2.我使用的是類Zend\Form\Fieldset如何自定義ZF2單選按鈕的HTML輸出?

在我的字段集類是這樣的:

$this->add(array( 
    'name' => 'type', 
    'type' => 'Zend\Form\Element\Radio', 
    'attributes' => array(
     'value' => $this->getType(), 
    ), 
    'options' => array( 
     'label' => 'Type', 
     'label_attributes' => array(
      'class' => '', 
     ), 
     'value_options' => array(
      '1' => 'Option 1', 
      '2' => 'Option 2', 
      '3' => 'Option 3' 
     ), 
    ), 
)); 

筆者認爲:

<div class="row"> 
    <div class="input-field col s6"> 
     <?php echo $this->formRadio($form->get('type')) ?> 
    </div> 
</div> 

而且它打印這樣的:

<div class="row"> 
    <div class="input-field col s6"> 
     <label class="radio"> 
      <input type="radio" name="type" value="1" checked="checked">Option 1 
     </label> 
     <label class="radio"> 
      <input type="radio" name="type" value="2">Option 2 
     </label> 
     <label class="radio"> 
      <input type="radio" name="type" value="3">Option 3 
     </label> 
    </div> 
</div> 

但我希望它打印象下面這樣:

<div class="row"> 
    <div class="input-field col s6"> 
     <p> 
      <input name="type" type="radio" id="option1" /> 
      <label for="option1">Option 1</label> 
     </p> 
     <p> 
      <input name="type" type="radio" id="option2" /> 
      <label for="option2">Option 2</label> 
     </p> 
     <p> 
      <input name="type" type="radio" id="option3" /> 
      <label for="option3">Option 3</label> 
     </p> 
    </div> 
</div> 

我需要改變,因爲我使用MaterializeCSS我的佈局,它只能這樣,我想。

UPDATE

我的解決辦法是:

<div class="row"> 
    <div class="input-field col s6"> 
     <?php $element = $form->get('type') ?> 
     <?php foreach ($element->getValueOptions() as $value => $label): ?> 
      <?php $checked = $value == $element->getValue() ? 'checked="true"' : ''; ?> 
      <p> 
       <input name="<?php echo $element->getName() ?>" type="radio" id="<?php echo $element->getName().$value ?>" value="<?php echo $value ?>" <?php echo $checked ?>> 
       <label for="<?php echo $element->getName().$value ?>"><?php echo $label ?></label> 
      </p> 
     <?php endforeach ?> 
    </div> 
</div> 

回答

2

如果你想你的表單元素不同的渲染,你應該sepperatly給他們打電話。

看看http://zf2cheatsheet.com/#form和向下滾動的助手的列表

+0

ZF文檔吸得很厲害,甚至需要第三方備忘...... :-( – LeandroCR

+0

沒有需要的cheatsheet,剛讀從代碼框架,真的不是那麼特別 – PAlphen