我尝试在视图中呈现表单输入,但不是分隔label
,errors
和widget
,而是呈现整个row
输入。
我的类型.php
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'label' => 'Email',
'required' => true,
])
->add('emailIsPublic', CheckboxType::class, [
'label' => 'Make e-mail public (can be seen by everyone)',
'required' => false,
'attr' => [
'class' => 'switch',
]
])
->add('submit', SubmitType::class, [
'label' => 'Save changes',
'attr' => [
'class' => 'btn btn-outline-primary float-right',
]
]);
}
我的视图.html.twig
{{ form_start(edit_form) }}
<div>
{{ form_label(edit_form.emailIsPublic) }}
</div>
<div>
{{ form_errors(edit_form.emailIsPublic) }}
</div>
<div>
{{ form_widget(edit_form.emailIsPublic) }}
</div>
{{ form_end(edit_form, {'render_rest': false}) }}
生成的 HTML
<form name="appbundle_my" method="post">
<div></div>
<div></div>
<div>
<div class="form-check">
<label class="form-check-label">
<input id="appbundle_my_emailIsPublic" name="appbundle_my[emailIsPublic]" class="switch form-check-input" value="1" type="checkbox"> Make e-mail public (can be seen by everyone)
</label>
</div>
</div>
</form>
正如我们在生成的 HTML 中看到的那样,form_label
它form_errors
是空的并form_widget
呈现form_row
应该呈现的内容。为什么?