Skip to content

[Form] Add default transformer to TextType field (and related) #2421

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

Merged
merged 1 commit into from
Dec 21, 2011

Conversation

stloyd
Copy link
Contributor

@stloyd stloyd commented Oct 17, 2011

Bug fix: yes&no (?)
Feature addition: yes (?)
BC break: no
Symfony2 tests pass: yes
Fixes the following tickets: #1962.

}

/**
* Transforms a string into a Boolean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy&paste rulz!!!oneoneone... ;-) Will replace later on :-)

@stloyd
Copy link
Contributor Author

stloyd commented Dec 19, 2011

@fabpot ping ;-)

@fabpot
Copy link
Member

fabpot commented Dec 19, 2011

Is it really needed? I have a feeling that it enforces unneeded constraints, but I can be wrong of course.

@hlecorche
Copy link
Contributor

It's needed because with TextType field, and without the ValueToStringTransformer, the user data (when sending the form) can be an array !!!

For example:

  • if there is a TextType field
  • and if there is a MaxLengthValidator
  • and if the user data (when sending the form) is an array
    So the exception "Expected argument of type string, array given in src\Symfony\Component\Validator\Constraints\MaxLengthValidator.php at line 40" is thrown

fabpot added a commit that referenced this pull request Dec 21, 2011
Commits
-------

49d2685 [Form] Add default validation to TextType field (and related)

Discussion
----------

[Form] Add default transformer to TextType field (and related)

Bug fix: yes&no (?)
Feature addition: yes (?)
BC break: no
Symfony2 tests pass: yes
Fixes the following tickets: #1962.

---------------------------------------------------------------------------

by stloyd at 2011/12/19 03:43:37 -0800

@fabpot ping ;-)

---------------------------------------------------------------------------

by fabpot at 2011/12/19 10:58:20 -0800

Is it really needed? I have a feeling that it enforces unneeded constraints, but I can be wrong of course.

---------------------------------------------------------------------------

by hlecorche at 2011/12/20 02:31:03 -0800

It's needed because with TextType field, and without the ValueToStringTransformer, the user data (when sending the form) can be an array !!!

For example:
- if there is a TextType field
- and if there is a MaxLengthValidator
- and if the user data (when sending the form) is an array
So the exception "Expected argument of type string, array given in src\Symfony\Component\Validator\Constraints\MaxLengthValidator.php at line 40" is thrown
@fabpot fabpot merged commit 49d2685 into symfony:master Dec 21, 2011
@arnaud-lb
Copy link
Contributor

This happens to break other client transformers, for instance a text field accepting a user name, with a UsernameToUserTransformer converting the user name to a user instance, wouldn't work anymore (example: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Form/DataTransformer/UsernameToUserTransformer.php )

@arnaud-lb
Copy link
Contributor

I've sent a PR adding a test for this regression: #3016

public function transform($value)
{
if (null === $value) {
return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if that's expected, but there is a little behavior change here.

Before adding this data transformer, empty strings were converted to NULL. It's not the case anymore.

Form::clientToNorm() returns NULL if the value is an empty string, if there is no client transformer.

Now that this client transformer is added by default on TextType, empty strings are not longer transformed to NULL.

@webmozart
Copy link
Contributor

What's the point of this addition?

@hlecorche Why should the browser submit an array?

@hlecorche
Copy link
Contributor

@bschussek A pirate or a bad behavior can send an array... and a error 500 is thrown...

@webmozart
Copy link
Contributor

Ok, thanks for the answer. See the referenced ticket for more information.

vicb added a commit to vicb/symfony that referenced this pull request Apr 27, 2012
…ony#2421)"

This reverts commit 7ea9c5b, reversing
changes made to 5803146.

Conflicts:

	src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValueToStringTransformerTest.php
@webmozart
Copy link
Contributor

This PR was reverted in #4831 because it does not provide a good solution and introduces more bugs (e.g. #4648). The required functionality needs to be solved in a different manner (see #4102).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants