Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Form] Fix Array to string conversion#20935
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
HeahDude commentedDec 15, 2016
@enumag Could you please first add a failing test case for this? Something like: $form =$this->factory->create('text','test');$form->submit(array());$this->assertSame('test',$form->getData());$this->assertFalse($form->isValid()); This listener sounds like a bad idea (also for performances), this should be checked in
symfony/src/Symfony/Component/Form/Form.php Line 1116 ind88f2a4
But since not compound forms can be submitted an array (like a multiple non expanded |
enumag commentedDec 15, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@HeahDude Good suggestions although far from solving the issue. The problem with failing test is that checking if the form is not valid is NOT enough. It will still cause the error when rendering the invalid form. But I can't add a test for rendering because the form component itself has no templates. I believe it's necessary to actually throw away the submitted value as it's not only invalid but also can't be used for rendering - the test should check that the value got trashed AND the form is invalid. Where do you think this should happen? (Probably based on the internal option.) |
HeahDude commentedDec 15, 2016
If my test above is green (which should not currently), then the issue should be solved, meaning we should prevent I've linked two places where I think we can handle this in the |
yceruto commentedDec 15, 2016
I guess that should be fixed in 2.7 base branch? |
yceruto commentedDec 15, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Note that |
xabbuh commentedDec 19, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@HeahDude We could introduce an option that can be used by form types to define the accepted types when the type is not compound. Before Symfony 4.0 the default value would be |
Tobion commentedOct 11, 2017
Related to#4102 |
| if (is_array($event->getData())) { | ||
| $event->setData(''); | ||
| $event->getForm()->addError( | ||
| newFormError('Invalid value.') |
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 suggest you to add error this way:
newFormError('This value should be of type {{ type }}.',null, ['{{ type }}' =>'string'])
Thus, it uses an already existing error message which is convenient for translations.
What do you think about it ?
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.
This solution is completely wrong. Read the comments above for details.
nicolas-grekas commentedNov 24, 2018
Closing in favor of#29307 |
…kas)This PR was merged into the 3.4 branch.Discussion----------[Form] Filter arrays out of scalar form types| Q | A| ------------- | ---| Branch? | 3.4| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#4102| License | MIT| Doc PR | -Replacesfix#20935Commits-------000e4aa [Form] Filter arrays out of scalar form types
…kas)This PR was merged into the 3.4 branch.Discussion----------[Form] Filter arrays out of scalar form types| Q | A| ------------- | ---| Branch? | 3.4| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |symfony/symfony#4102| License | MIT| Doc PR | -Replacesfixsymfony/symfony#20935Commits-------000e4aab5e [Form] Filter arrays out of scalar form types
Uh oh!
There was an error while loading.Please reload this page.
Let's have a form with a textarea and some other field. Open the form in browser, open debug tools and add
[]to the name attribute of the textarea. Put some invalid value into the other field. The result is an Array to string conversion onthis line in TwigBridge. It's of course because the submitted value of the textarea is actually an array because of our manipulation via debug tools.No matter what the user input is Symfony should not fail on an error like this which is why I consider this a bug.
I'm not quite sure if overwriting the submitted value and adding an error in a PRE_SUBMIT event is a good fix for this issue. Please share any suggestions.
Of course the current implementation is far from ready - this should be solved by a form extension for all fields except ChoiceType with multiple option and CollectionType. Those on the other hand need some protection against non-array values.
By the way adding a
Type("string")constraint to the field does not help (obviously).