My Symfony form uses the BirthdayType field. Its default behaviour is to add three separate dropdowns for the month, day and year. I added the text widget option as described here in order to have three separate text input fields. My question is: how can I also generate labels for each of those input fields?
The generated HTML is:
<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 (excerpt):
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('birthDate', BirthdayType::class, ['widget' => 'text'])
->add('next', SubmitType::class);
}
You can add labels in the options like you did with the 'widget'
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);
}
Also see https://symfony.com/doc/current/forms.html#field-type-options
you should make a custom twig :
{#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 %}
and add this line in the config.yml :
# app/config/config.yml
twig:
form_themes:
- 'form/birdhdate.html.twig'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.