如何将元素添加到使用 formbuilder (Symfony) 创建的表单中?
How can I add an element into form created with formbuilder (Symfony)?
我在 Symfony4 中通过 formbuilder 创建了一个表单:
$formBuilder->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control')));
这给了我以下输出:
<div>
<label for="form_username" class="required">Username</label>
<input type="text" id="form_username" name="form[username]" class="form-control">
</div>
我想在输入字段后添加一行,所以结果如下所示:
<div>
<label for="form_username" class="required">Username</label>
<input type="text" id="form_username" name="form[username]" required="required" class="form-control">
<span class="info" data-name="form[username]">
</div>
我的做法:
$formBuilder->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control', 'after' => "<span class='info' data-name='form[username]'></span>")));
但这不正确
您可以在 Twig 中打印单个标签和小部件。
<div>
{{ form_label(form.fieldName) }}
{{ form_widget(form.fieldName) }}
<span class="info" data-name="form[username]">
</div>
正如@Ahmed EBEN HASSINE 所说,更喜欢树枝形式的主题https://symfony.com/doc/current/form/form_customization.html#adding-help-messages
在一个模板内
{% use 'form_div_layout.html.twig' with form_widget_simple as base_form_widget_simple %}
{% block form_widget_simple %}
{{ block('base_form_widget_simple') }}
<span class="info" data-name="{{ full_name }}">
{% endblock %}
{# rest of your code #}
为所有模板创建一个基础文件
{# template/form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}
{% block form_widget_simple %}
{{ parent() }}
<span class="info" data-name="{{ full_name }}">
{% endblock %}
并在您的配置中声明它
# config/packages/twig.yaml
twig:
form_themes:
- 'form/fields.html.twig'
我在 Symfony4 中通过 formbuilder 创建了一个表单:
$formBuilder->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control')));
这给了我以下输出:
<div>
<label for="form_username" class="required">Username</label>
<input type="text" id="form_username" name="form[username]" class="form-control">
</div>
我想在输入字段后添加一行,所以结果如下所示:
<div>
<label for="form_username" class="required">Username</label>
<input type="text" id="form_username" name="form[username]" required="required" class="form-control">
<span class="info" data-name="form[username]">
</div>
我的做法:
$formBuilder->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control', 'after' => "<span class='info' data-name='form[username]'></span>")));
但这不正确
您可以在 Twig 中打印单个标签和小部件。
<div>
{{ form_label(form.fieldName) }}
{{ form_widget(form.fieldName) }}
<span class="info" data-name="form[username]">
</div>
正如@Ahmed EBEN HASSINE 所说,更喜欢树枝形式的主题https://symfony.com/doc/current/form/form_customization.html#adding-help-messages
在一个模板内
{% use 'form_div_layout.html.twig' with form_widget_simple as base_form_widget_simple %}
{% block form_widget_simple %}
{{ block('base_form_widget_simple') }}
<span class="info" data-name="{{ full_name }}">
{% endblock %}
{# rest of your code #}
为所有模板创建一个基础文件
{# template/form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}
{% block form_widget_simple %}
{{ parent() }}
<span class="info" data-name="{{ full_name }}">
{% endblock %}
并在您的配置中声明它
# config/packages/twig.yaml
twig:
form_themes:
- 'form/fields.html.twig'