-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Fix ChoiceType terminology #6144
Comments
Can I add to this and suggest that the terminology for the When setting
But to then also suggest that:
https://symfony.com/doc/current/reference/forms/types/choice.html#choice-attr But isn’t that a bit of a contradiction. As by setting the attributes per key aren’t you therefore not setting the attributes the same for each choice? Got caught up a little by this today. One would think that by providing an array it would apply to each choice no?
But in-fact the second point is correct and the array has to map to the individual choices:
Be interested on your thoughts on this @webmozart. I can try and submit a pull request if so. Bit of a novice when it comes to contributing to projects like this but I can give it a go 😃 . |
This PR was merged into the 3.4 branch. Discussion ---------- [Form] Fixed some phpdocs | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | symfony/symfony-docs#6393 ref symfony/symfony-docs#6144, symfony/symfony-docs#6297, #14050 Commits ------- b9162e8 [Form] Fixed some phpdocs
Currently, the ChoiceType documentation is IMO not clearly worded. I know that readability is important, but since this type is quite flexible and complex, being precise is even more important. All code examples and descriptions should be updated to use the following terms:
choices
array. E.g.:true
,false
,object (Category)
, ...choices
array.'0'
,'1'
,'yes'
,'no'
, ... Values must be free of duplicates, since they are used to identify the corresponding choice when submitting the form.This naming is important since those values are passed from one callable to another:
choice_value
callable with signaturefunction ($choice)
receives the choice and returns the string value.choice_label
callable with signaturefunction ($choice, $key, $value)
receives the choice, the key and the value and returns a string label. The default value isfunction ($choice, $key, $value) => $key
, i.e. the key of thechoices
array is used as label by default.choice_name
callable has the same signature aschoice_label
and outputs the form name used for checkboxes/radio buttons.choice_attr
callable has the same signature aschoice_label
and outputs the HTML attributes.Furthermore, the
choice_loader
is not completely accurate:I would rephrase this. The choice loader is used to load choices from a data source that can be queried with a query language, such as a database or a search engine. When the choice field is displayed, the full list is loaded, but when it is submitted, only the submitted value is looked up. A second benefit is that different fields that use the same
ChoiceLoader
instance use the same cached choice list, reducing N queries to 1.Before/After code examples:
Before:
After:
Before:
After:
I also recommend to add type hints in the advanced example:
Before:
After:
The text was updated successfully, but these errors were encountered: