我的Symfony表单使用BirthdayType字段。它的默认行为是为月,日和年添加三个单独的下拉菜单。我按照here所述添加了文本小部件选项,以便具有三个单独的文本输入字段。我的问题是:如何为每个输入字段生成标签?
生成的HTML是:
<div id="user_birthDate">
<input type="text" id="user_birthDate_month" name="user[birthDate][month]" required="required">
<input type="text" id="user_birthDate_day" name="user[birthDate][day]" required="required">
<input type="text" id="user_birthDate_year" name="user[birthDate][year]" required="required">
</div>
FormBuilder(节选):
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('birthDate', BirthdayType::class, ['widget' => 'text'])
->add('next', SubmitType::class);
}
答案 0 :(得分:0)
您可以像使用“小部件”一样在选项中添加标签
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('birthDate', BirthdayType::class, [
'widget' => 'text',
'label' => 'Birthday label that you want to show',
])
->add('next', SubmitType::class);
}
另请参见https://symfony.com/doc/current/forms.html#field-type-options
答案 1 :(得分:0)
您应该制作一个自定义树枝:
{#birthdate.html.twig !#}
{% block birthday_widget %}
{% spaceless %}
<label> month </label>
<input type="text" id="user_birthDate_month" name="user[birthDate][month]" required="required">
<label> day </label>
<input type="text" id="user_birthDate_day" name="user[birthDate][day]" required="required">
<label> year </label>
<input type="text" id="user_birthDate_year" name="user[birthDate][year]" required="required">
{% endspaceless %}
{% endblock %}
并将此行添加到config.yml中:
# app/config/config.yml
twig:
form_themes:
- 'form/birdhdate.html.twig'