-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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 Array to string conversion #20935
Conversation
@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 555 in d88f2a4
symfony/src/Symfony/Component/Form/Form.php Line 1116 in d88f2a4
But since not compound forms can be submitted an array (like a multiple non expanded |
@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.) |
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 |
I guess that should be fixed in 2.7 base branch? |
Note that |
@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 |
Related to #4102 |
if (is_array($event->getData())) { | ||
$event->setData(''); | ||
$event->getForm()->addError( | ||
new FormError('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:
new FormError('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.
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 | - Replaces fix #20935 Commits ------- 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 | - Replaces fix symfony/symfony#20935 Commits ------- 000e4aab5e [Form] Filter arrays out of scalar form types
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 on this 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).