2016-11-01 8 views
0

私は次のようにラジオボタンが定義されfooを設定している:それは、このように呼ばれていますビュースクリプトでラジオボタンセットのコンテナのクラスをZF2フォームに設定する方法は?私のフォームで

$this->add(
    [ 
     'type' => 'radio', 
     'name' => 'foo', 
     'options' => [ 
      'label' => 'foo', 
      'value_options' => [ 
       [ 
        'value' => Foo::BAR, 
        'label' => 'bar' 
       ], 
       [ 
        'value' => Foo::BUZ, 
        'label' => 'buz' 
       ] 
      ], 
      'label_attributes' => [ 
       'class' => 'col-md-12', 
      ] 
     ], 
     'attributes' => [ 
      'class' => 'field-foo' 
     ] 
    ]); 

を:

$this->formRow($myFieldset->get('foo')); 

だから私はこのHTMLを取得する:

<fieldset> 
    <legend>foo</legend> 
    <label class="col-md-12"> 
     <input type="radio" value="bar" class="field-foo" name="my_fieldset[foo]">bar 
    </label> 
    <label class="col-md-12"> 
     <input type="radio" value="buz" class="field-foo" name="my_fieldset[foo]">buz 
    </label> 
</fieldset> 

今度は、必要に応じてこのラジオボタンを設定します。

label.required:before { 
    content: '* '; 
    color: #ff0000; 
} 

この場合、私はこれを行う方法を

fieldset > legend.required:before, /*or*/ 
fieldset.required > legend:before { 
    content: '* '; 
    color: #ff0000; 
} 

を定義するために、legendまたは少なくともfieldsetにアクセスする必要があります。input[type="text"]フィールドのために私はlabelを経由していることを管理しますか? Zend Framework 2で設定したラジオボタンの要素fieldset/のクラスを設定する方法は?

答えて

1

オプションでこれを行うことはできません。しかし、あなたはあなたの入力をテンプレートにパーシャルを使用することができます。このファイルで

$this->formRow($myFieldset->get('foo'), null, null, 'path/to/partials/some-partial-file.phtml'); 

を、あなたはあなたの入力がレンダリングされる方法をカスタマイズすることができ、何が必要追加:

<?php 
/** 
* @var \Zend\Form\Element\Radio $element 
*/ 

$element_options = $element->getValueOptions(); 
$element_attributes = $element->getAttributes(); 
$element_value = $element->getValue(); 
?> 
<fieldset> 
    <legend><?php echo $element->getLabel(); ?> * </legend> 
    <?php foreach($element_options as $key => $value) { ?> 

    <label class="col-md-12"> 
     <input type="radio" value="<?php echo $value['value']; ?>" class="field-foo" name="<?php echo $element_attributes['name']; ?>[]"><?php echo $value['value']; ?> 
    </label> 
    <?php } ?> 
</fieldset> 

Zendのは、$をパーシャルに渡されますフォーム入力要素を保持する要素変数

関連する問題