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 https://github.com/symfony/symfony/pull/29182
Commits
-------
4939f0e323
Fix handling of empty_data's \Closure value in Date/Time form types
This commit is contained in:
commit
28eedb8cef
@ -107,7 +107,17 @@ class DateTimeType extends AbstractType
|
||||
'invalid_message_parameters',
|
||||
]));
|
||||
|
||||
if (isset($emptyData['date'])) {
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$lazyEmptyData = static function ($option) use ($emptyData) {
|
||||
return static function (FormInterface $form) use ($emptyData, $option) {
|
||||
$emptyData = $emptyData($form->getParent());
|
||||
|
||||
return isset($emptyData[$option]) ? $emptyData[$option] : '';
|
||||
};
|
||||
};
|
||||
|
||||
$dateOptions['empty_data'] = $lazyEmptyData('date');
|
||||
} elseif (isset($emptyData['date'])) {
|
||||
$dateOptions['empty_data'] = $emptyData['date'];
|
||||
}
|
||||
|
||||
@ -126,7 +136,9 @@ class DateTimeType extends AbstractType
|
||||
'invalid_message_parameters',
|
||||
]));
|
||||
|
||||
if (isset($emptyData['time'])) {
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$timeOptions['empty_data'] = $lazyEmptyData('time');
|
||||
} elseif (isset($emptyData['time'])) {
|
||||
$timeOptions['empty_data'] = $emptyData['time'];
|
||||
}
|
||||
|
||||
|
@ -81,14 +81,28 @@ class DateType extends AbstractType
|
||||
// so we need to handle the cascade setting here
|
||||
$emptyData = $builder->getEmptyData() ?: [];
|
||||
|
||||
if (isset($emptyData['year'])) {
|
||||
$yearOptions['empty_data'] = $emptyData['year'];
|
||||
}
|
||||
if (isset($emptyData['month'])) {
|
||||
$monthOptions['empty_data'] = $emptyData['month'];
|
||||
}
|
||||
if (isset($emptyData['day'])) {
|
||||
$dayOptions['empty_data'] = $emptyData['day'];
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$lazyEmptyData = static function ($option) use ($emptyData) {
|
||||
return static function (FormInterface $form) use ($emptyData, $option) {
|
||||
$emptyData = $emptyData($form->getParent());
|
||||
|
||||
return isset($emptyData[$option]) ? $emptyData[$option] : '';
|
||||
};
|
||||
};
|
||||
|
||||
$yearOptions['empty_data'] = $lazyEmptyData('year');
|
||||
$monthOptions['empty_data'] = $lazyEmptyData('month');
|
||||
$dayOptions['empty_data'] = $lazyEmptyData('day');
|
||||
} else {
|
||||
if (isset($emptyData['year'])) {
|
||||
$yearOptions['empty_data'] = $emptyData['year'];
|
||||
}
|
||||
if (isset($emptyData['month'])) {
|
||||
$monthOptions['empty_data'] = $emptyData['month'];
|
||||
}
|
||||
if (isset($emptyData['day'])) {
|
||||
$dayOptions['empty_data'] = $emptyData['day'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['invalid_message'])) {
|
||||
|
@ -76,7 +76,17 @@ class TimeType extends AbstractType
|
||||
// so we need to handle the cascade setting here
|
||||
$emptyData = $builder->getEmptyData() ?: [];
|
||||
|
||||
if (isset($emptyData['hour'])) {
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$lazyEmptyData = static function ($option) use ($emptyData) {
|
||||
return static function (FormInterface $form) use ($emptyData, $option) {
|
||||
$emptyData = $emptyData($form->getParent());
|
||||
|
||||
return isset($emptyData[$option]) ? $emptyData[$option] : '';
|
||||
};
|
||||
};
|
||||
|
||||
$hourOptions['empty_data'] = $lazyEmptyData('hour');
|
||||
} elseif (isset($emptyData['hour'])) {
|
||||
$hourOptions['empty_data'] = $emptyData['hour'];
|
||||
}
|
||||
|
||||
@ -143,14 +153,18 @@ class TimeType extends AbstractType
|
||||
$builder->add('hour', self::$widgets[$options['widget']], $hourOptions);
|
||||
|
||||
if ($options['with_minutes']) {
|
||||
if (isset($emptyData['minute'])) {
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$minuteOptions['empty_data'] = $lazyEmptyData('minute');
|
||||
} elseif (isset($emptyData['minute'])) {
|
||||
$minuteOptions['empty_data'] = $emptyData['minute'];
|
||||
}
|
||||
$builder->add('minute', self::$widgets[$options['widget']], $minuteOptions);
|
||||
}
|
||||
|
||||
if ($options['with_seconds']) {
|
||||
if (isset($emptyData['second'])) {
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$secondOptions['empty_data'] = $lazyEmptyData('second');
|
||||
} elseif (isset($emptyData['second'])) {
|
||||
$secondOptions['empty_data'] = $emptyData['second'];
|
||||
}
|
||||
$builder->add('second', self::$widgets[$options['widget']], $secondOptions);
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
class DateTimeTypeTest extends BaseTypeTest
|
||||
{
|
||||
@ -608,6 +609,9 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
]);
|
||||
$form->submit(null);
|
||||
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$emptyData = $emptyData($form);
|
||||
}
|
||||
$this->assertSame($emptyData, $form->getViewData());
|
||||
$this->assertEquals($expectedData, $form->getNormData());
|
||||
$this->assertEquals($expectedData, $form->getData());
|
||||
@ -616,11 +620,17 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
public function provideEmptyData()
|
||||
{
|
||||
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '2018-11-11 21:23');
|
||||
$lazyEmptyData = static function (FormInterface $form) {
|
||||
return $form->getConfig()->getCompound() ? ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']] : '2018-11-11T21:23:00';
|
||||
};
|
||||
|
||||
return [
|
||||
'Simple field' => ['single_text', '2018-11-11T21:23:00', $expectedData],
|
||||
'Compound text field' => ['text', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
|
||||
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
|
||||
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
|
||||
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
|
||||
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Intl\Util\IntlTestHelper;
|
||||
|
||||
class DateTypeTest extends BaseTypeTest
|
||||
@ -985,6 +986,9 @@ class DateTypeTest extends BaseTypeTest
|
||||
]);
|
||||
$form->submit(null);
|
||||
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$emptyData = $emptyData($form);
|
||||
}
|
||||
$this->assertSame($emptyData, $form->getViewData());
|
||||
$this->assertEquals($expectedData, $form->getNormData());
|
||||
$this->assertEquals($expectedData, $form->getData());
|
||||
@ -993,11 +997,17 @@ class DateTypeTest extends BaseTypeTest
|
||||
public function provideEmptyData()
|
||||
{
|
||||
$expectedData = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-11-11 00:00:00');
|
||||
$lazyEmptyData = static function (FormInterface $form) {
|
||||
return $form->getConfig()->getCompound() ? ['year' => '2018', 'month' => '11', 'day' => '11'] : '2018-11-11';
|
||||
};
|
||||
|
||||
return [
|
||||
'Simple field' => ['single_text', '2018-11-11', $expectedData],
|
||||
'Compound text fields' => ['text', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
|
||||
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
|
||||
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
|
||||
'Compound text fields lazy' => ['text', $lazyEmptyData, $expectedData],
|
||||
'Compound choice fields lazy' => ['choice', $lazyEmptyData, $expectedData],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
class TimeTypeTest extends BaseTypeTest
|
||||
{
|
||||
@ -785,6 +786,9 @@ class TimeTypeTest extends BaseTypeTest
|
||||
]);
|
||||
$form->submit(null);
|
||||
|
||||
if ($emptyData instanceof \Closure) {
|
||||
$emptyData = $emptyData($form);
|
||||
}
|
||||
$this->assertSame($emptyData, $form->getViewData());
|
||||
$this->assertEquals($expectedData, $form->getNormData());
|
||||
$this->assertEquals($expectedData, $form->getData());
|
||||
@ -793,11 +797,17 @@ class TimeTypeTest extends BaseTypeTest
|
||||
public function provideEmptyData()
|
||||
{
|
||||
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '1970-01-01 21:23');
|
||||
$lazyEmptyData = static function (FormInterface $form) {
|
||||
return $form->getConfig()->getCompound() ? ['hour' => '21', 'minute' => '23'] : '21:23';
|
||||
};
|
||||
|
||||
return [
|
||||
'Simple field' => ['single_text', '21:23', $expectedData],
|
||||
'Compound text field' => ['text', ['hour' => '21', 'minute' => '23'], $expectedData],
|
||||
'Compound choice field' => ['choice', ['hour' => '21', 'minute' => '23'], $expectedData],
|
||||
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],
|
||||
'Compound text field lazy' => ['text', $lazyEmptyData, $expectedData],
|
||||
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user