[Form] Add labels option to DateType to customize year/month/day sub-field labels

This commit is contained in:
Guillaume VDP
2026-03-10 14:28:22 +01:00
parent 83516f87d1
commit eaced1f587
3 changed files with 46 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ CHANGELOG
* Deprecate passing boolean as the second argument of `ValidatorExtension` and `FormTypeValidatorExtension`'s constructors; pass a `ViolationMapperInterface` instead
* Add argument `$violationMapper` to `ValidatorExtensionTrait` and `TypeTestCase`'s `getExtensions()` methods
* Add default `min`/`max` attributes to `BirthdayType` when `widget` is `single_text`
* Add `labels` option to `DateType` to customize the year, month and day sub-field labels
8.0
---

View File

@@ -147,6 +147,10 @@ class DateType extends AbstractType
$yearOptions[$passOpt] = $monthOptions[$passOpt] = $dayOptions[$passOpt] = $options[$passOpt];
}
$yearOptions['label'] = $options['labels']['year'];
$monthOptions['label'] = $options['labels']['month'];
$dayOptions['label'] = $options['labels']['day'];
$builder
->add('year', self::WIDGETS[$options['widget']], $yearOptions)
->add('month', self::WIDGETS[$options['widget']], $monthOptions)
@@ -269,6 +273,11 @@ class DateType extends AbstractType
];
};
$labelsNormalizer = static fn (Options $options, array $labels) => array_replace(
['year' => null, 'month' => null, 'day' => null],
array_filter($labels, static fn ($label) => null !== $label)
);
$format = static fn (Options $options) => 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
$resolver->setDefaults([
@@ -282,6 +291,7 @@ class DateType extends AbstractType
'view_timezone' => null,
'calendar' => null,
'placeholder' => $placeholderDefault,
'labels' => [],
'html5' => true,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
@@ -301,6 +311,8 @@ class DateType extends AbstractType
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
$resolver->setNormalizer('labels', $labelsNormalizer);
$resolver->setAllowedTypes('labels', 'array');
$resolver->setAllowedValues('input', [
'datetime',

View File

@@ -1204,6 +1204,39 @@ class DateTypeTest extends BaseTypeTestCase
$this->assertSame('113-03-31', $form->getViewData());
}
public function testPassLabelsAsArray()
{
$view = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'choice',
'labels' => [
'year' => 'Year label',
'month' => 'Month label',
'day' => 'Day label',
],
])
->createView();
$this->assertSame('Year label', $view['year']->vars['label']);
$this->assertSame('Month label', $view['month']->vars['label']);
$this->assertSame('Day label', $view['day']->vars['label']);
}
public function testPassLabelsAsPartialArray()
{
$view = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'choice',
'labels' => [
'year' => 'Year label',
'day' => 'Day label',
],
])
->createView();
$this->assertSame('Year label', $view['year']->vars['label']);
$this->assertNull($view['month']->vars['label']);
$this->assertSame('Day label', $view['day']->vars['label']);
}
protected function getTestOptions(): array
{
return ['widget' => 'choice'];