From ef8057742d68a0c2ce3055e0b05b0debfb033c29 Mon Sep 17 00:00:00 2001 From: Guillaume VDP Date: Thu, 14 Nov 2024 12:25:52 +0100 Subject: [PATCH 1/3] [Form] BirthdayType has automatic attr when widget is single_text --- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Form/Extension/Core/Type/BirthdayType.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 3b1fabfd17afd..5586091df0543 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -9,6 +9,7 @@ CHANGELOG * Add `LazyChoiceLoader` and `choice_lazy` option in `ChoiceType` for loading and rendering choices on demand * Use `form.post_set_data` instead of `form.pre_set_data` in `ResizeFormListener` * Change the priority of `DataCollectorListener` from 255 to -255 + * `BirthdayType` will now have default `attr` values when in `widget` option is set to `single_text` 7.1 --- diff --git a/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php b/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php index 651b880d86c77..31a98f3895c4b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Form\Extension\Core\Type; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class BirthdayType extends AbstractType @@ -35,4 +37,17 @@ public function getBlockPrefix(): string { return 'birthday'; } + + public function buildView(FormView $view, FormInterface $form, array $options): void + { + parent::buildView($view, $form, $options); + $currentMonth = date('m'); + $currentDay = date('d'); + if (!isset($view->vars['attr']['min'])) { + $view->vars['attr']['min'] = sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay); + } + if (!isset($view->vars['attr']['max'])) { + $view->vars['attr']['max'] = sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay); + } + } } From 6c3676ab0f18a3b4b3cd2db5f10eb3a783aa2af5 Mon Sep 17 00:00:00 2001 From: Guillaume VDP Date: Thu, 14 Nov 2024 15:51:07 +0100 Subject: [PATCH 2/3] [Form] BirthdayType add tests --- .../Extension/Core/Type/BirthdayTypeTest.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php index 53e5c959cee95..6aff9f9ce5d0b 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php @@ -21,7 +21,7 @@ class BirthdayTypeTest extends DateTypeTest { public const TESTED_TYPE = BirthdayType::class; - public function testSetInvalidYearsOption() + public function testSetInvalidYearsOption(): void { $this->expectException(InvalidOptionsException::class); $this->factory->create(static::TESTED_TYPE, null, [ @@ -29,4 +29,35 @@ public function testSetInvalidYearsOption() 'widget' => 'choice', ]); } + + public function testWidgetSingleTextHasDefaultAttrMinMax(): void + { + $currentMonth = date('m'); + $currentDay = date('d'); + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'widget' => 'single_text', + ]); + $formView = $form->createView(); + $options = $form->getConfig()->getOptions(); + $expectedMin = sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay); + $expectedMax = sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay); + $this->assertEquals($expectedMin, $formView->vars['attr']['min']); + $this->assertEquals($expectedMax, $formView->vars['attr']['max']); + } + + public function testWidgetSingleTextDoesntRemoveUserAttr(): void + { + $expectedMin = date('Y-m-d', strtotime('10 years ago')); + $expectedMax = date('Y-m-d', strtotime('1 years ago')); + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'widget' => 'single_text', + 'attr' => [ + 'min' => $expectedMin, + 'max' => $expectedMax + ] + ]); + $formView = $form->createView(); + $this->assertEquals($expectedMin, $formView->vars['attr']['min']); + $this->assertEquals($expectedMax, $formView->vars['attr']['max']); + } } From 358ae60c1a5c2a86a45e00f1c79b70c0f845c42b Mon Sep 17 00:00:00 2001 From: Guillaume VDP Date: Thu, 14 Nov 2024 16:40:52 +0100 Subject: [PATCH 3/3] [Form] BirthdayType cleaning code --- .../Component/Form/Extension/Core/Type/BirthdayType.php | 9 ++------- .../Form/Tests/Extension/Core/Type/BirthdayTypeTest.php | 6 +++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php b/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php index 31a98f3895c4b..c2c595a3281d1 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php @@ -40,14 +40,9 @@ public function getBlockPrefix(): string public function buildView(FormView $view, FormInterface $form, array $options): void { - parent::buildView($view, $form, $options); $currentMonth = date('m'); $currentDay = date('d'); - if (!isset($view->vars['attr']['min'])) { - $view->vars['attr']['min'] = sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay); - } - if (!isset($view->vars['attr']['max'])) { - $view->vars['attr']['max'] = sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay); - } + $view->vars['attr']['min'] ??= sprintf('%d-%s-%s', reset($options['years']), $currentMonth, $currentDay); + $view->vars['attr']['max'] ??= sprintf('%d-%s-%s', end($options['years']), $currentMonth, $currentDay); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php index 6aff9f9ce5d0b..8cf2d0d3c6415 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php @@ -21,7 +21,7 @@ class BirthdayTypeTest extends DateTypeTest { public const TESTED_TYPE = BirthdayType::class; - public function testSetInvalidYearsOption(): void + public function testSetInvalidYearsOption() { $this->expectException(InvalidOptionsException::class); $this->factory->create(static::TESTED_TYPE, null, [ @@ -30,7 +30,7 @@ public function testSetInvalidYearsOption(): void ]); } - public function testWidgetSingleTextHasDefaultAttrMinMax(): void + public function testWidgetSingleTextHasDefaultAttrMinMax() { $currentMonth = date('m'); $currentDay = date('d'); @@ -45,7 +45,7 @@ public function testWidgetSingleTextHasDefaultAttrMinMax(): void $this->assertEquals($expectedMax, $formView->vars['attr']['max']); } - public function testWidgetSingleTextDoesntRemoveUserAttr(): void + public function testWidgetSingleTextDoesntRemoveUserAttr() { $expectedMin = date('Y-m-d', strtotime('10 years ago')); $expectedMax = date('Y-m-d', strtotime('1 years ago'));