Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Closed
enumag wants to merge1 commit intosymfony:masterfromenumag:patch-20

Conversation

@enumag
Copy link
Contributor

@enumagenumag commentedDec 15, 2016
edited
Loading

QA
Branch?master
Bug fix?yes
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets
LicenseMIT
Doc PR

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 aType("string") constraint to the field does not help (obviously).

lobodol and ndench reacted with thumbs up emoji
@enumagenumag changed the titleAdd error on array value in text field[Form] Fix Array to string converionDec 15, 2016
@enumagenumag changed the title[Form] Fix Array to string converion[Form] Fix Array to string conversionDec 15, 2016
@HeahDude
Copy link
Contributor

@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 inForm::submit() instead IMHO, like this is done for compound forms here

thrownewTransformationFailedException('Compound forms expect an array or NULL on submission.');
, or maybe here
privatefunctionviewToNorm($value)
when no view transformers are set.

But since not compound forms can be submitted an array (like a multiple non expandedChoiceType as you pointed out), it means that we should use an internal option to define the behavior (accepting array or not), anyway I can't find a better solution right now.

@enumag
Copy link
ContributorAuthor

enumag commentedDec 15, 2016
edited
Loading

@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
Copy link
Contributor

If my test above is green (which should not currently), then the issue should be solved, meaning we should preventarray() === $form->getData().

I've linked two places where I think we can handle this in theForm class. But others can have a better idea.

@yceruto
Copy link
Member

I guess that should be fixed in 2.7 base branch?

@yceruto
Copy link
Member

yceruto commentedDec 15, 2016
edited
Loading

Note thatFileType uses themultiple attribute as well, it don't use data transformer and expect anarray() also.

@xabbuh
Copy link
Member

xabbuh commentedDec 19, 2016
edited
Loading

@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 benull which doesn't do anything, but we would already deprecate thenull value. In 4.0, we would then disallownull and makestring the new default value.

@Tobion
Copy link
Contributor

Related to#4102

if (is_array($event->getData())) {
$event->setData('');
$event->getForm()->addError(
newFormError('Invalid value.')

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 ?

Copy link
ContributorAuthor

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
Copy link
Member

Closing in favor of#29307

nicolas-grekas added a commit that referenced this pull requestDec 8, 2018
…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
symfony-splitter pushed a commit to symfony/form that referenced this pull requestDec 8, 2018
…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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@lobodollobodollobodol left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

2.8

Development

Successfully merging this pull request may close these issues.

9 participants

@enumag@HeahDude@yceruto@xabbuh@Tobion@nicolas-grekas@lobodol@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp