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 cf4fc31

Browse filesBrowse files
committed
bug #34123 [Form] Fix handling of empty_data's \Closure value in Date/Time form types (yceruto)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] Fix handling of empty_data's \Closure value in Date/Time form types | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #33188 | License | MIT | Doc PR | - Basically this would solve the posibility to pass a `\Closure` to the `empty_data` option for Date/Time form types. > https://symfony.com/doc/current/reference/forms/types/form.html#empty-data > If a form is compound, you can set empty_data as an array, object or **closure**. See the [How to Configure empty Data](https://symfony.com/doc/current/form/use_empty_data.html) for a Form Class article for more details about these options. Also related to symfony/symfony#29182 Commits ------- 4939f0e323 Fix handling of empty_data's \Closure value in Date/Time form types
2 parents 99f6cf6 + 36301cb commit cf4fc31
Copy full SHA for cf4fc31

File tree

Expand file treeCollapse file tree

6 files changed

+83
-13
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+83
-13
lines changed

‎Extension/Core/Type/DateTimeType.php

Copy file name to clipboardExpand all lines: Extension/Core/Type/DateTimeType.php
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
107107
'invalid_message_parameters',
108108
]));
109109

110-
if (isset($emptyData['date'])) {
110+
if ($emptyData instanceof \Closure) {
111+
$lazyEmptyData = static function ($option) use ($emptyData) {
112+
return static function (FormInterface $form) use ($emptyData, $option) {
113+
$emptyData = $emptyData($form->getParent());
114+
115+
return isset($emptyData[$option]) ? $emptyData[$option] : '';
116+
};
117+
};
118+
119+
$dateOptions['empty_data'] = $lazyEmptyData('date');
120+
} elseif (isset($emptyData['date'])) {
111121
$dateOptions['empty_data'] = $emptyData['date'];
112122
}
113123

@@ -126,7 +136,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
126136
'invalid_message_parameters',
127137
]));
128138

129-
if (isset($emptyData['time'])) {
139+
if ($emptyData instanceof \Closure) {
140+
$timeOptions['empty_data'] = $lazyEmptyData('time');
141+
} elseif (isset($emptyData['time'])) {
130142
$timeOptions['empty_data'] = $emptyData['time'];
131143
}
132144

‎Extension/Core/Type/DateType.php

Copy file name to clipboardExpand all lines: Extension/Core/Type/DateType.php
+22-8Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,28 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8181
// so we need to handle the cascade setting here
8282
$emptyData = $builder->getEmptyData() ?: [];
8383

84-
if (isset($emptyData['year'])) {
85-
$yearOptions['empty_data'] = $emptyData['year'];
86-
}
87-
if (isset($emptyData['month'])) {
88-
$monthOptions['empty_data'] = $emptyData['month'];
89-
}
90-
if (isset($emptyData['day'])) {
91-
$dayOptions['empty_data'] = $emptyData['day'];
84+
if ($emptyData instanceof \Closure) {
85+
$lazyEmptyData = static function ($option) use ($emptyData) {
86+
return static function (FormInterface $form) use ($emptyData, $option) {
87+
$emptyData = $emptyData($form->getParent());
88+
89+
return isset($emptyData[$option]) ? $emptyData[$option] : '';
90+
};
91+
};
92+
93+
$yearOptions['empty_data'] = $lazyEmptyData('year');
94+
$monthOptions['empty_data'] = $lazyEmptyData('month');
95+
$dayOptions['empty_data'] = $lazyEmptyData('day');
96+
} else {
97+
if (isset($emptyData['year'])) {
98+
$yearOptions['empty_data'] = $emptyData['year'];
99+
}
100+
if (isset($emptyData['month'])) {
101+
$monthOptions['empty_data'] = $emptyData['month'];
102+
}
103+
if (isset($emptyData['day'])) {
104+
$dayOptions['empty_data'] = $emptyData['day'];
105+
}
92106
}
93107

94108
if (isset($options['invalid_message'])) {

‎Extension/Core/Type/TimeType.php

Copy file name to clipboardExpand all lines: Extension/Core/Type/TimeType.php
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7676
// so we need to handle the cascade setting here
7777
$emptyData = $builder->getEmptyData() ?: [];
7878

79-
if (isset($emptyData['hour'])) {
79+
if ($emptyData instanceof \Closure) {
80+
$lazyEmptyData = static function ($option) use ($emptyData) {
81+
return static function (FormInterface $form) use ($emptyData, $option) {
82+
$emptyData = $emptyData($form->getParent());
83+
84+
return isset($emptyData[$option]) ? $emptyData[$option] : '';
85+
};
86+
};
87+
88+
$hourOptions['empty_data'] = $lazyEmptyData('hour');
89+
} elseif (isset($emptyData['hour'])) {
8090
$hourOptions['empty_data'] = $emptyData['hour'];
8191
}
8292

@@ -143,14 +153,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
143153
$builder->add('hour', self::$widgets[$options['widget']], $hourOptions);
144154

145155
if ($options['with_minutes']) {
146-
if (isset($emptyData['minute'])) {
156+
if ($emptyData instanceof \Closure) {
157+
$minuteOptions['empty_data'] = $lazyEmptyData('minute');
158+
} elseif (isset($emptyData['minute'])) {
147159
$minuteOptions['empty_data'] = $emptyData['minute'];
148160
}
149161
$builder->add('minute', self::$widgets[$options['widget']], $minuteOptions);
150162
}
151163

152164
if ($options['with_seconds']) {
153-
if (isset($emptyData['second'])) {
165+
if ($emptyData instanceof \Closure) {
166+
$secondOptions['empty_data'] = $lazyEmptyData('second');
167+
} elseif (isset($emptyData['second'])) {
154168
$secondOptions['empty_data'] = $emptyData['second'];
155169
}
156170
$builder->add('second', self::$widgets[$options['widget']], $secondOptions);

‎Tests/Extension/Core/Type/DateTimeTypeTest.php

Copy file name to clipboardExpand all lines: Tests/Extension/Core/Type/DateTimeTypeTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\FormError;
15+
use Symfony\Component\Form\FormInterface;
1516

1617
class DateTimeTypeTest extends BaseTypeTest
1718
{
@@ -608,6 +609,9 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
608609
]);
609610
$form->submit(null);
610611

612+
if ($emptyData instanceof \Closure) {
613+
$emptyData = $emptyData($form);
614+
}
611615
$this->assertSame($emptyData, $form->getViewData());
612616
$this->assertEquals($expectedData, $form->getNormData());
613617
$this->assertEquals($expectedData, $form->getData());
@@ -616,11 +620,17 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
616620
public function provideEmptyData()
617621
{
618622
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '2018-11-11 21:23');
623+
$lazyEmptyData = static function (FormInterface $form) {
624+
return $form->getConfig()->getCompound() ? ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']] : '2018-11-11T21:23:00';
625+
};
619626

620627
return [
621628
'Simple field' => ['single_text', '2018-11-11T21:23:00', $expectedData],
622629
'Compound text field' => ['text', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
623630
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
631+
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
632+
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
633+
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
624634
];
625635
}
626636
}

‎Tests/Extension/Core/Type/DateTypeTest.php

Copy file name to clipboardExpand all lines: Tests/Extension/Core/Type/DateTypeTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1515
use Symfony\Component\Form\FormError;
16+
use Symfony\Component\Form\FormInterface;
1617
use Symfony\Component\Intl\Util\IntlTestHelper;
1718

1819
class DateTypeTest extends BaseTypeTest
@@ -985,6 +986,9 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
985986
]);
986987
$form->submit(null);
987988

989+
if ($emptyData instanceof \Closure) {
990+
$emptyData = $emptyData($form);
991+
}
988992
$this->assertSame($emptyData, $form->getViewData());
989993
$this->assertEquals($expectedData, $form->getNormData());
990994
$this->assertEquals($expectedData, $form->getData());
@@ -993,11 +997,17 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
993997
public function provideEmptyData()
994998
{
995999
$expectedData = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-11-11 00:00:00');
1000+
$lazyEmptyData = static function (FormInterface $form) {
1001+
return $form->getConfig()->getCompound() ? ['year' => '2018', 'month' => '11', 'day' => '11'] : '2018-11-11';
1002+
};
9961003

9971004
return [
9981005
'Simple field' => ['single_text', '2018-11-11', $expectedData],
9991006
'Compound text fields' => ['text', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
10001007
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
1008+
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
1009+
'Compound text fields lazy' => ['text', $lazyEmptyData, $expectedData],
1010+
'Compound choice fields lazy' => ['choice', $lazyEmptyData, $expectedData],
10011011
];
10021012
}
10031013
}

‎Tests/Extension/Core/Type/TimeTypeTest.php

Copy file name to clipboardExpand all lines: Tests/Extension/Core/Type/TimeTypeTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1515
use Symfony\Component\Form\FormError;
16+
use Symfony\Component\Form\FormInterface;
1617

1718
class TimeTypeTest extends BaseTypeTest
1819
{
@@ -785,6 +786,9 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
785786
]);
786787
$form->submit(null);
787788

789+
if ($emptyData instanceof \Closure) {
790+
$emptyData = $emptyData($form);
791+
}
788792
$this->assertSame($emptyData, $form->getViewData());
789793
$this->assertEquals($expectedData, $form->getNormData());
790794
$this->assertEquals($expectedData, $form->getData());
@@ -793,11 +797,17 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
793797
public function provideEmptyData()
794798
{
795799
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '1970-01-01 21:23');
800+
$lazyEmptyData = static function (FormInterface $form) {
801+
return $form->getConfig()->getCompound() ? ['hour' => '21', 'minute' => '23'] : '21:23';
802+
};
796803

797804
return [
798805
'Simple field' => ['single_text', '21:23', $expectedData],
799806
'Compound text field' => ['text', ['hour' => '21', 'minute' => '23'], $expectedData],
800807
'Compound choice field' => ['choice', ['hour' => '21', 'minute' => '23'], $expectedData],
808+
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
809+
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
810+
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
801811
];
802812
}
803813
}

0 commit comments

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