Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c36395f

Browse filesBrowse files
feature #63631 [Form] Add labels option to DateType to customize year/month/day sub-field labels (guillaumeVDP)
This PR was merged into the 8.1 branch. Discussion ---------- [Form] Add `labels` option to DateType to customize year/month/day sub-field labels | Q | A | ------------- | --- | Branch? | 8.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #59825 | License | MIT **Problem** When using `DateType` (or `BirthdayType`) with `widget: choice` or `widget: text`, the year/month/day sub-field labels are auto-generated from the field names ("Year", "Month", "Day") with no way to customize or disable them. **Solution** Add a `labels` option to `DateType`, following the same pattern as the existing `labels` option in `DateIntervalType`. Accepted values: - `[]` (default) - auto-generates labels from field names - `array` - per-field control, unspecified fields fall back to null Usage: ``` $builder->add('dueDate', DateType::class, [ 'widget' => 'choice', 'labels' => [ 'year' => 'Birth year', 'month' => 'Birth month', 'day' => 'Birth day', ], ]); // partial array — only override what you need $builder->add('dueDate', DateType::class, [ 'widget' => 'choice', 'labels' => [ 'year' => 'Birth year', ], ]); ``` Since BirthdayType extends DateType, it inherits this option automatically. Commits ------- 814c851 [Form] Add `labels` option to DateType to customize year/month/day sub-field labels
2 parents 2d351db + 814c851 commit c36395f
Copy full SHA for c36395f

3 files changed

+46Lines changed: 46 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Symfony/Component/Form/CHANGELOG.md‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Deprecate passing boolean as the second argument of `ValidatorExtension` and `FormTypeValidatorExtension`'s constructors; pass a `ViolationMapperInterface` instead
1111
* Add argument `$violationMapper` to `ValidatorExtensionTrait` and `TypeTestCase`'s `getExtensions()` methods
1212
* Add default `min`/`max` attributes to `BirthdayType` when `widget` is `single_text`
13+
* Add `labels` option to `DateType` to customize the year, month and day sub-field labels
1314

1415
8.0
1516
---
Collapse file

‎src/Symfony/Component/Form/Extension/Core/Type/DateType.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
147147
$yearOptions[$passOpt] = $monthOptions[$passOpt] = $dayOptions[$passOpt] = $options[$passOpt];
148148
}
149149

150+
$yearOptions['label'] = $options['labels']['year'];
151+
$monthOptions['label'] = $options['labels']['month'];
152+
$dayOptions['label'] = $options['labels']['day'];
153+
150154
$builder
151155
->add('year', self::WIDGETS[$options['widget']], $yearOptions)
152156
->add('month', self::WIDGETS[$options['widget']], $monthOptions)
@@ -269,6 +273,11 @@ public function configureOptions(OptionsResolver $resolver): void
269273
];
270274
};
271275

276+
$labelsNormalizer = static fn (Options $options, array $labels) => array_replace(
277+
['year' => null, 'month' => null, 'day' => null],
278+
array_filter($labels, static fn ($label) => null !== $label)
279+
);
280+
272281
$format = static fn (Options $options) => 'single_text' === $options['widget'] ? self::HTML5_FORMAT : self::DEFAULT_FORMAT;
273282

274283
$resolver->setDefaults([
@@ -282,6 +291,7 @@ public function configureOptions(OptionsResolver $resolver): void
282291
'view_timezone' => null,
283292
'calendar' => null,
284293
'placeholder' => $placeholderDefault,
294+
'labels' => [],
285295
'html5' => true,
286296
// Don't modify \DateTime classes by reference, we treat
287297
// them like immutable value objects
@@ -301,6 +311,8 @@ public function configureOptions(OptionsResolver $resolver): void
301311

302312
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
303313
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
314+
$resolver->setNormalizer('labels', $labelsNormalizer);
315+
$resolver->setAllowedTypes('labels', 'array');
304316

305317
$resolver->setAllowedValues('input', [
306318
'datetime',
Collapse file

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,39 @@ public function testSetDataWithCustomCalendarOption()
12041204
$this->assertSame('113-03-31', $form->getViewData());
12051205
}
12061206

1207+
public function testPassLabelsAsArray()
1208+
{
1209+
$view = $this->factory->create(static::TESTED_TYPE, null, [
1210+
'widget' => 'choice',
1211+
'labels' => [
1212+
'year' => 'Year label',
1213+
'month' => 'Month label',
1214+
'day' => 'Day label',
1215+
],
1216+
])
1217+
->createView();
1218+
1219+
$this->assertSame('Year label', $view['year']->vars['label']);
1220+
$this->assertSame('Month label', $view['month']->vars['label']);
1221+
$this->assertSame('Day label', $view['day']->vars['label']);
1222+
}
1223+
1224+
public function testPassLabelsAsPartialArray()
1225+
{
1226+
$view = $this->factory->create(static::TESTED_TYPE, null, [
1227+
'widget' => 'choice',
1228+
'labels' => [
1229+
'year' => 'Year label',
1230+
'day' => 'Day label',
1231+
],
1232+
])
1233+
->createView();
1234+
1235+
$this->assertSame('Year label', $view['year']->vars['label']);
1236+
$this->assertNull($view['month']->vars['label']);
1237+
$this->assertSame('Day label', $view['day']->vars['label']);
1238+
}
1239+
12071240
protected function getTestOptions(): array
12081241
{
12091242
return ['widget' => 'choice'];

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.