您可以使用下一个关闭表单语句来防止呈现在表单中定义但在模板中未描述的表单字段:
{{ form_end(form, {'render_rest': false}) }}
例如,我们定义下一种形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'id',
HiddenType::class,
array(
'required' => false
)
)
->add(
'name',
TextType::class,
array(
'required' => false
)
)
->add(
'comment',
TextType::class,
array(
'required' => false
)
)
->add(
'amount',
TextType::class,
array(
'required' => false
)
);
}
对于这个表单,我们描述了下一个模板,但我们不想渲染 field id
,所以我们可以render_rest
在form_end
block 中使用 option 来省略 field 的渲染id
:
{# render opening form tag #}
{{ form_start(form) }}
{# render field with label #}
{{ form_row(form.name) }}
{# render only field #}
{{ form_widget(form.comment) }}
{# render only label #}
{{ form_label(form.amount) }}
{# render only field #}
{{ form_widget(form.amount) }}
{# if csrf token is enabled for form render field #}
{% if form._token is defined %}
{{ form_widget(form._token) }}
{% endif %}
{# render closing form tag and do not render rest form elements #}
{{ form_end(form, {'render_rest': false}) }}