-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix handling of empty_data's \Closure value in Date/Time form types #34123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fa43849
to
8a5798a
Compare
@xabbuh done, thanks! |
src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Outdated
Show resolved
Hide resolved
8a5798a
to
4939f0e
Compare
@@ -126,7 +136,9 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |||
'invalid_message_parameters', | |||
])); | |||
|
|||
if (isset($emptyData['time'])) { | |||
if ($emptyData instanceof \Closure) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about doing the same as what you did in the DateType
then?
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');
} else {
if (isset($emptyData['date'])) {
$dateOptions['empty_data'] = $emptyData['date'];
}
if (isset($emptyData['time'])) {
$timeOptions['empty_data'] = $emptyData['time'];
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To do that the $timeOptions
initialization should be moved before the first empty data definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see there are conflicting comments from Nicolas and Christian, so maybe we should always set $lazyEmptyData
base on the instance of once, then test if $lazyEmptyData
is null to define the options.
Thank you @yceruto. |
…/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 #29182 Commits ------- 4939f0e Fix handling of empty_data's \Closure value in Date/Time form types
Basically this would solve the posibility to pass a
\Closure
to theempty_data
option for Date/Time form types.Also related to #29182