2012-02-27 9 views
2

現在、私は、ドロップダウンを選択し、私のカテゴリエンティティからの生成、フォーム、持っている:Symfony2のフォーム:生成のチェックボックス

 $builder 
      ->add('category', 'entity', 
        array('class' => 'TrackerMembersBundle:Category', 
          'property' => 'title',)); 

は、今私が代わりにチェックボックスを生成したい、私は複数を選択することができますオプション...私はsymfony2のドキュメントを検索しましたが、Entity.Anyのアイデアから直接行う簡単な方法が見つかりませんでしたか?

答えて

11

オプション配列(クラスとプロパティの最後のもの)に'multiple' => trueを追加すると、複数選択できます。

form themingを使用してchoice_widgetブロックを上書きできます。このような

何かがうまくいくかもしれない:

{% block choice_widget %} 
{% spaceless %} 
    {% for choice, label in choices %} 
     <label> 
      <input type="checkbox" value="{{ choice }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}> 
      {{ label|trans }} 
     </label> 
    {% endfor %} 
{% endspaceless %} 
{% endblock choice_widget %} 
0

EntityTypeが親ChoiceTypeを持っているので、あなたがchoice種類からいずれかのオプションを使用することができます

 $builder 
     ->add('category', 'checkbox', 
       array('class' => 'TrackerMembersBundle:Category', 
         'property' => 'title',));# 

http://symfony.com/doc/current/reference/forms/types/checkbox.html

+0

私はこれを試してみました。オプション "class"、 "property"、 "multiple"は存在しません – Confidence

+0

これは動作しません。彼は 'entity'のformTypeを必要とし、' checkbox'の型は必要としません。 – Problematic

10

を試してみてください。チェックボックスをレンダリングするでしょう、次のようにあなたのcategoryフィールドを構成し、あなたの場合は

、:

$builder 
    ->add('category', 'entity', array(
     'class' => 'TrackerMembersBundle:Category', 
     'property' => 'title', 
     'multiple' => true, 
     'expanded' => true, 
    ) 
); 

multipleとオプションでexpandedの使用を注意してください。

+0

複数と拡張はドロップダウンに影響しませんでした。 – Confidence

+0

それは普通ではありません:)私はあなたに答える前に自分のプロジェクトでテストしましたが、うまくいきました。メッセージや何かエラーがありますか? – Florian

関連する問題