Skip to content

[Form] empty_data in ChoiceType can be a string what must not be called #25896

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

Closed
peter-gribanov opened this issue Jan 23, 2018 · 9 comments
Closed

Comments

@peter-gribanov
Copy link
Contributor

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.8.33

Example problem:

->add('order', ChoiceType::class, [
    'expanded' => true,
    'choices' => [
        'view' => 'news.search.order.view',
        'name' => 'news.search.order.name',
        'date' => 'news.search.order.date',
    ],
    'empty_data' => 'date',
])

In this case, i get an error at this line:

Warning: date() expects parameter 1 to be string, object given

@xabbuh
Copy link
Member

xabbuh commented Jan 23, 2018

You are right. But I don't see what we can do here without break BC. The solution for you would be to pass an anonymous function that just returns the string.

@xabbuh
Copy link
Member

xabbuh commented Jan 23, 2018

In other places, we deprecated the ability to pass callables as string to avoid such issues. That's something we could do here in 4.1 too so that we can remove support for it in 5.0.

@peter-gribanov peter-gribanov changed the title empty_data in ChoiceType can be a string what must not be called [Form] empty_data in ChoiceType can be a string what must not be called Jan 25, 2018
@derrabus
Copy link
Member

@xabbuh Do we still have more places like that? That sounds like an ugly pitfall in general.

@xabbuh
Copy link
Member

xabbuh commented Jan 25, 2018

I think it's very likely that we have similar code in other places.

@peter-gribanov
Copy link
Contributor Author

peter-gribanov commented Jan 25, 2018

Maybe, a similar problem can be here, here and here.

@derrabus
Copy link
Member

@peter-gribanov I totally agree about FileType, that's the very same mechanism.

I'm not sure about DefaultChoiceListFactory though. Here it's either a callable or an array and callable arrays should have a different structure than the mapping arrays that we expect here.

@peter-gribanov
Copy link
Contributor Author

I'm not sure about DefaultChoiceListFactory though. Here it's either a callable or an array and callable arrays should have a different structure than the mapping arrays that we expect here.

@derrabus it may seem absurd, but it's still possible.

This code seems innocuous:

$builder->add('body', ChoiceType::class, array(
    'attr' => ['title' => 'sum'],
));

But everything can turn out to be very unexpected if the project has such class:

class Title
{
    public static function sum($x, $y)
    {
        return sprintf('Sum x + y = %d', $x + $y);
    }
}

@derrabus
Copy link
Member

@peter-gribanov No, because to call the class in your example, the array would be ['title', 'sum'] and not ['title' => 'sum'].

@peter-gribanov
Copy link
Contributor Author

peter-gribanov commented Jan 25, 2018

@derrabus exactly. sorry 🙇

fabpot added a commit that referenced this issue Feb 1, 2018
…e (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fixed empty data on expanded ChoiceType and FileType

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25896
| License       | MIT
| Doc PR        | ~

Alternative of #25924.

I don't think we have to do this by adding overhead in master and getting it properly fixed in 2 years.

This is bug I've introduced while fixing another bug #17822. Which then has been replicated in #20418 and #24993.

I propose instead to clean up the code for all LTS, since this is a wrong behaviour that has never been documented, and most of all unreliable.
The `empty_data` can by anything in the view format as long as the reverse view transformation supports it. Even an object derived from the data class could be invokable.

I think we should remain consistent with https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Form/Form.php#L615.

Commits
-------

9722c06 [Form] Fixed empty data on expanded ChoiceType and FileType
@fabpot fabpot closed this as completed Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants