2012-09-20 3 views
3

OK 난 2 개 옵션 Symfony2 선택 양식 분할

$builder->add('type', 'choice', array(
    'label' => 'User type', 
    'choices' => array('1' => 'Customer', '2' => 'Supplier'), 
    'expanded' => true, 
    'multiple' => false, 
    'required' => false, 
)); 

으로 선택 양식을 가지고 그리고 난 같은 것을 얻기 위해 나뭇 가지를 사용하여보기에서 옵션을 분할 할 수 있도록 :

{{ form_widget(form.type/choice_1/) }} 
some html stuf 
{{ form_widget(form.type/choice_2/) }} 

하나를 수혈?

답변

9

서식을 서식 파일에 추가해야합니다. 당신을 위해 https://github.com/phiamo/MopaBootstrapBundle/blob/master/Resources/views/Form/fields.html.twig

이 필드한다 : http://symfony.com/doc/current/cookbook/form/form_customization.html

다음

당신이있어 여러 예 : 여기에 문서의

{% block choice_widget_expanded %} 
{% spaceless %} 
<div {{ block('widget_container_attributes') }}> 
{% for child in form %} 
    <label class="{{ (multiple ? 'checkbox' : 'radio') ~ (widget_type ? ' ' ~ widget_type : '') ~ (inline is defined and inline ? ' inline' : '') }}"> 
     {{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }} 
     {{ child.vars.label|trans({}, translation_domain) }} 
    </label> 
{% endfor %} 
</div> 
{% endspaceless %} 
{% endblock choice_widget_expanded %} 

당신은 그냥두고 원하는대로 수행 할 수 있습니다

: {{ form_widget(child, {'attr': {'class': attr.widget_class|default('')}}) }} 혼자
  1. 파일을 만들어야합니다. YourBundle/Resources/Form/fields.html.twig
  2. 위 코드를 붙여 넣으십시오.
  3. 다음 양식에 테마를 추가하십시오 : {% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
  4. 그리고 rock'n'roll 준비!
+0

고마워. 그것은 작동합니다! –

2
필드의

정의 수 :

{{ form_widget(form.type.0) }}{{ form_label(form.type.0) }} 
    some html stuf 
{{ form_widget(form.type.1) }}{{ form_label(form.type.0) }} 

변수 필드의 수 :

예를 들어

:

{% for i in 0..form.type|length-1 %} 
    {{ form_widget(form.type[i]) }} 
    {{ form_label(form.type[i]) }} 
{% endfor %} 

그리고 우리는하지 않도록 선택에 ID가있는 경우

$typeChoice = [ 
    "choice 1" => 2, 
    "choice 2" => 5 
] 

{% for type in form.type %} 
    {{ form_label(type) }} 
    {{ form_widget(type) }} 
{% endfor %}