【发布时间】:2019-07-21 06:38:27
【问题描述】:
我尝试覆盖 symfony 表单主题。它有效,这是个好消息! 但是我想改变里面标签的位置。
我的表单生成器:
$builder->add('phone', IntegerType::class, [
'label' => 'Telefonnummer',
'constraints' => [
new NotBlank(),
],
'attr' => [
'class' => 'input-field__input',
],
'label_attr' => [
'class' => 'input-field__label',
]
]);
我重写的 integer_widget:
{% block integer_widget %}
<div class="input-field">
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
我的预期(简化示例):
<div class="input-field">
<label>Telefonnummer</label>
<input/>
</div>
我得到了什么:
<label>Telefonnummer</label>
<div class="input-field">
<input/>
</div>
标签在 div 之前,不在里面。但我需要它!
我尝试过的(有效,但在我看来它是一团糟):
{% block integer_widget %}
<div class="input-field">
{% block form_label %}
{% endblock %}
<label class="{{ label_attr.class }}">{{ label }}</label>
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
这样看起来就像我预期的那样。但这只是一种解决方法,因为我通过添加一个空的“form_label”块删除了原始标签。
在 symfony/twig 的规则内有更好的解决方案吗?
【问题讨论】: