我怎样才能把我必填字段的星号放在我的标签上? (交响乐形式)
How can I put the asterisk of my required field on my label? (Symfony form)
我正在使用 Symfony 3,但我的表单遇到了一些问题。
当我创建一个带有不需要字段的 Symfony 表单时,这是我的代码:
我创建了表格:
$form = $this->createFormBuilder()
->add('prenom' TextType::class, array(
'label' => 'Votre prénom',
'required' => false
)
->getForm();
这是我认为该字段的代码:
{{ form_label(form.prenom) }}
{{ form_errors(form.prenom) }}
{{ form_widget(form.prenom) }}
这是我的 HTML :
<label class="control-label" for="contact_prenom">Votre prénom</label>
<input type="text" id="contact_prenom" name="contact[prenom]" class="form-control"/>
现在,如果我在我的 FormBuilder 上不使用 'require' => false
,那么我得到的是 HTML:
<label class="control-label required" for="contact_prenom">Votre prénom</label>
<sup class="required" title="Champ obligatoire">
<i class="fa fa-asterisk"></i>
</sup>
<input type="text" id="contact_prenom" name="contact[prenom]" required="required" class="form-control" />
是否可以控制 "sup" 标签,使星号 *
可以与我的标签一起使用?
我想我可以用 jQuery 做到这一点,但我想知道 是否可以在我的表单生成器或 Twig 中做到这一点?
是的,你可以覆盖 symfony 用来呈现你的小部件的 twig 模板或块,看看:
http://symfony.com/doc/current/templating/overriding.html
对于您的情况,您正在寻找
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
这将是您要覆盖的块:
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}
你甚至可以只用 CSS
label.required:before {
content: "* ";
}
从 Symfony 5.1 开始,您可以执行以下操作
->add('name', TextType::class, [
'label' => 'Name <span class="badge badge-danger badge-pill">Required</span>',
'label_html' => true
])
label_html
(bool) 属性 将允许 HTML 直接注入标签并呈现在表单输出上。
文档 - https://symfony.com/doc/current/reference/forms/types/form.html#label-html
我正在使用 Symfony 3,但我的表单遇到了一些问题。
当我创建一个带有不需要字段的 Symfony 表单时,这是我的代码:
我创建了表格:
$form = $this->createFormBuilder()
->add('prenom' TextType::class, array(
'label' => 'Votre prénom',
'required' => false
)
->getForm();
这是我认为该字段的代码:
{{ form_label(form.prenom) }}
{{ form_errors(form.prenom) }}
{{ form_widget(form.prenom) }}
这是我的 HTML :
<label class="control-label" for="contact_prenom">Votre prénom</label>
<input type="text" id="contact_prenom" name="contact[prenom]" class="form-control"/>
现在,如果我在我的 FormBuilder 上不使用 'require' => false
,那么我得到的是 HTML:
<label class="control-label required" for="contact_prenom">Votre prénom</label>
<sup class="required" title="Champ obligatoire">
<i class="fa fa-asterisk"></i>
</sup>
<input type="text" id="contact_prenom" name="contact[prenom]" required="required" class="form-control" />
是否可以控制 "sup" 标签,使星号 *
可以与我的标签一起使用?
我想我可以用 jQuery 做到这一点,但我想知道 是否可以在我的表单生成器或 Twig 中做到这一点?
是的,你可以覆盖 symfony 用来呈现你的小部件的 twig 模板或块,看看: http://symfony.com/doc/current/templating/overriding.html
对于您的情况,您正在寻找
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
这将是您要覆盖的块:
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}
你甚至可以只用 CSS
label.required:before {
content: "* ";
}
从 Symfony 5.1 开始,您可以执行以下操作
->add('name', TextType::class, [
'label' => 'Name <span class="badge badge-danger badge-pill">Required</span>',
'label_html' => true
])
label_html
(bool) 属性 将允许 HTML 直接注入标签并呈现在表单输出上。
文档 - https://symfony.com/doc/current/reference/forms/types/form.html#label-html