问题
I have this form generation code..:
$form = $this->createFormBuilder($task)
->add('mode', 'choice', array(
'label' => false,
'choices' => array(
'mode1' => '1',
'mode2' => '2',
'mode3' => '3',
),
'multiple' => false,
'expanded' => true,
'required' => true,
))
->getForm();
The problem is that rendered form has choices (radio inputs) inline, without <br/>
tags between them.
Also, i cannot find how do i render form with a twig template so do not touch PHP code, for example, how can i specially 'decorate' each choice in my radio buttons list.
<div style='margin:25px'>
{{ form_start(form_options) }}
{{ form_widget(form_options) }}
{{ form_end(form_options) }}
</div>
How can i extend this?
回答1:
You can render each form field as you like. Do not use {{ form_widget(form_options) }}
use {{ form_row (form.fieldName) }}
then you can add html and style as you like : This is an exemple :
<div class="checkbox-list">
<label class="checkbox-inline col-md-6">
<div class="checker" id="uniform-inlineCheckbox21">
<span class="">
{{form_row(form.fieldName)}}
</span>
</div>
fieldName
</label>
You can also pass a list of object and create manually your input :
{% for language in languages %}
<div class="checkbox-list">
<label class="checkbox-inline col-md-6">
<div class="checker" id="uniform-inlineCheckbox21">
<span class="">
<input type="checkbox" name="langues[]" id="course{{loop.index}}" value="{{language.id}}" data-title="{{language.designation}}">
</span>
</div>
{{language.designation}}
</label>
</div>
{% endfor %}
Last solution you can use form_theme and override the choices widget user by symfony : http://symfony.com/fr/doc/current/cookbook/form/form_customization.html
回答2:
Quickest Solution
Add this to your CSS:
label:after { content:"\a"; white-space:pre; }
Dirty Solution
Render each radiobutton in Twig individually like this:
{{ form_widget(form.formField.0) }}{{ form_label(form.formField.0) }}<br>
{{ form_widget(form.formField.1) }}{{ form_label(form.formField.1) }}
Professional Solution
Customize the form theme, as explained at https://symfony.com/doc/current/form/form_customization.html
Drawback: If Symfony's default theme changes in the future, you will miss it, since you're overriding it.
来源:https://stackoverflow.com/questions/29370381/newline-br-between-radio-choices-in-symfony-form