-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Comments
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 There is (at least) 2 ways to achieve this:
|
@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. |
@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? |
And it seems there are some type adjustements made. Without digging too much into details I saw this fix within the ChoiceList: I guess you might experience only issues with the float type. Thank you again for your support! |
@peterrehm this is the choice index, not the choice value |
I saw this, but from the tests with it in the actual app there must some casts be going on. |
The point regarding validation is clear now. This solely depends on the optional strict option. Therefore it has matched. |
As promised I just added a docs PR. |
@peterrehm Some more explanation: PHP arrays can only use integers or strings as array keys. Since Fixing #4067 will help to solve your problem without having to explicitly create a ChoiceList instance (which will be done internally). |
Thank you for the information, I figured this out. So #4067 is definitely a good improvement, it makes it much more intuitive. |
…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
I have a numeric choice type in my application.
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.
The text was updated successfully, but these errors were encountered: