Skip to content
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

[Form] Fix handling of empty_data's \Closure value in Date/Time form types #34123

Merged
merged 1 commit into from Feb 3, 2020

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Oct 25, 2019

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 for a Form Class article for more details about these options.

Also related to #29182

@yceruto
Copy link
Member Author

yceruto commented Nov 3, 2019

@xabbuh done, thanks!

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

if (isset($emptyData['time'])) {
if ($emptyData instanceof \Closure) {
Copy link
Member

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'];
    }
}

Copy link
Contributor

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.

Copy link
Contributor

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.

@fabpot
Copy link
Member

fabpot commented Feb 3, 2020

Thank you @yceruto.

fabpot added a commit that referenced this pull request Feb 3, 2020
…/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
@fabpot fabpot merged commit 4939f0e into symfony:3.4 Feb 3, 2020
@yceruto yceruto deleted the lazy_empty_data branch February 3, 2020 17:49
This was referenced Feb 29, 2020
@fabpot fabpot mentioned this pull request Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants