我在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>")));
但这是不正确的
答案 0 :(得分:1)
您可以在Twig中打印单个标签和小部件。
<div>
{{ form_label(form.fieldName) }}
{{ form_widget(form.fieldName) }}
<span class="info" data-name="form[username]">
</div>
答案 1 :(得分:1)
正如@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'