Skip to content

[Form] Cast on choice type #11849

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
peterrehm opened this issue Sep 4, 2014 · 10 comments
Closed

[Form] Cast on choice type #11849

peterrehm opened this issue Sep 4, 2014 · 10 comments
Labels

Comments

@peterrehm
Copy link
Contributor

I have a numeric choice type in my application.

$builder
        ->add('test', 'choice', array('choices' => array('1' => 1, '0.9' => 0.9));

The property test is of type float.

Since the Form component populates on every submit the property test with '0.9' (string)
but in the entity there was 0.9 (float) doctrine detects the change and updates the entity
even though this is not necessary.

What do you think about a option to the choice field to cast to integer or float? I see several
use cases where this makes sense and I think in a lot of applications such updates are executed
without any notice.

@stof
Copy link
Member

stof commented Sep 4, 2014

The type is not only important for Doctrine but also for the Form type (detecting whether the choice is selected also uses strict comparison). So if you edit an object whch contains the 0.9 value, the '0.9' choice will not be selected currently.

There is (at least) 2 ways to achieve this:

@peterrehm
Copy link
Contributor Author

@stof Awesome - thank you for the detailed reply. I will try option 1 tomorrow, if it is that easy my issue is solved and I think I will add an hint to the docs.

@fabpot fabpot added the Form label Sep 5, 2014
@peterrehm
Copy link
Contributor Author

@stof I can confirm this works as you described it. However when investigating this I found that the Choice Validation Constraint seems not to do a strict comparison. Do you know if this is actually intented?

@peterrehm
Copy link
Contributor Author

And it seems there are some type adjustements made. Without digging too much into details I saw this fix within the ChoiceList:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php#L462

I guess you might experience only issues with the float type. Thank you again for your support!

@stof
Copy link
Member

stof commented Sep 8, 2014

@peterrehm this is the choice index, not the choice value

@peterrehm
Copy link
Contributor Author

I saw this, but from the tests with it in the actual app there must some casts be going on.

@peterrehm
Copy link
Contributor Author

The point regarding validation is clear now. This solely depends on the optional strict option. Therefore it has matched.

@peterrehm
Copy link
Contributor Author

As promised I just added a docs PR.

@webmozart
Copy link
Contributor

@peterrehm Some more explanation: PHP arrays can only use integers or strings as array keys. Since 0.9 is not a valid integer, it is stored as string and returned to your application as such.

Fixing #4067 will help to solve your problem without having to explicitly create a ChoiceList instance (which will be done internally).

@peterrehm
Copy link
Contributor Author

Thank you for the information, I figured this out. So #4067 is definitely a good improvement, it makes it much more intuitive.

weaverryan added a commit to symfony/symfony-docs that referenced this issue Oct 2, 2014
…rrehm)

This PR was submitted for the 2.5 branch but it was merged into the 2.3 branch instead (closes #4241).

Discussion
----------

[Form] Added information about float choice lists

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | no
| Applies to    | all
| Fixed tickets | -

Added an information about the possibility to have a choice list with float values.

Relates to symfony/symfony#11849

Commits
-------

81e3728 Added information about float choice lists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants