Skip to content

Fixed the params in ChoiceType methods #9835

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
wants to merge 3 commits into from

Conversation

javiereguiluz
Copy link
Member

This finishes #9173.

@@ -23,7 +23,7 @@ If an array, the keys of the ``choices`` array must be used as keys::
'Maybe' => null,
),
'choices_as_values' => true,
'choice_attr' => function($val, $key, $index) {
'choice_attr' => function($category, $key, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case the first argument will be one of choices values true | false | null. So I think, it should be called $choiceValue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to do that change ... but then 'choice_attr' => function($choiceValue, $key, $value) is going to look confusing. choiceValue and value? What's the difference?

@@ -20,7 +20,7 @@ more control::
'maybe' => null,
),
'choices_as_values' => true,
'choice_label' => function ($value, $key, $index) {
'choice_label' => function ($category, $key, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here ($choiceValue, $key, $value).

@@ -26,7 +26,7 @@ Take the following example::
'1 month' => new \DateTime('+1 month'),
),
'choices_as_values' => true,
'group_by' => function($value, $key, $index) {
'group_by' => function($vategory, $key, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here ($choiceValue, $key, $value).

@@ -26,7 +26,7 @@ Take the following example::
'1 month' => new \DateTime('+1 month'),
),
'choices_as_values' => true,
'group_by' => function($value, $key, $index) {
'group_by' => function($vategory, $key, $value) {
if ($value <= new \DateTime('+3 days')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here the comparison should be against the first argument $choiceValue. I will check that for 2.8. But it's definitely true for 4.0. The first argument of the callback is the model data. The third $value is just a string from request.

/** @var Category $category */
return strtoupper($category->getName());
},
'choice_attr' => function($category, $key, $index) {
'choice_attr' => function($category, $key, $value) {
return ['class' => 'category_'.strtolower($category->getName())];
},

Copy link
Contributor

Choose a reason for hiding this comment

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

empty line

Copy link
Contributor

@vudaltsov vudaltsov left a comment

Choose a reason for hiding this comment

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

Sorry, found some other things that could be fixed in this PR. And some other improvements I will do in a new one.

@@ -20,7 +20,7 @@ more control::
'maybe' => null,
),
'choices_as_values' => true,
'choice_label' => function ($value, $key, $index) {
'choice_label' => function ($choiceValue, $key, $value) {
if ($value == true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that is makes more sense to check true === $choiceValue here.

@vudaltsov
Copy link
Contributor

@javiereguiluz , let's finish this? :)
Any objections to my last comment?

@xabbuh , could you review this PR, please?

@javiereguiluz
Copy link
Member Author

@vudaltsov I thought you were going to create a new PR instead of this one ... so I was waiting :) I'm going to finish and merge this so you can create a new PR. Thanks!

javiereguiluz added a commit that referenced this pull request May 29, 2018
This PR was squashed before being merged into the 2.8 branch (closes #9835).

Discussion
----------

Fixed the params in ChoiceType methods

This finishes #9173.

Commits
-------

b14cf70 Fixed the params in ChoiceType methods
@javiereguiluz
Copy link
Member Author

This is now merged! @vudaltsov when you create your PR, please make any change needed to create variables easy to understand. E.g. $choiceValue -> is the value chosen by the user or the value shown in the choice list? Better use $selectedValue or $selectedIndex, etc. Thanks!

@vudaltsov
Copy link
Contributor

I think that $choiceValue is understandable enough because that's the value from the choices option. We can rename it only when we know what it is in terms of model data (like $category).

@vudaltsov
Copy link
Contributor

@yceruto , okay! I will do that in my PR.

@javiereguiluz
Copy link
Member Author

I still think it's not understandable. I can't understand anything here --> function ($choice, $index, $value)

Please, let's pick variable names that the reader cannot mistake. For example, which of the following names best describe the previous arguments?

function ($selectedChoice, $choiceIndex, $choiceValue)
function ($availableChoice, $selectedIndex, $selectedValue)
function ($availableChoice, $choiceIndex, $choiceValue)
etc.

@vudaltsov
Copy link
Contributor

vudaltsov commented May 29, 2018

If we want to be verbose: function ($choiceValue, $choiceKey, $requestValue).
The last argument is the value from $_REQUEST (or Request $request), most of the time nobody cares about it.
key is better than index because it obviously might be non-numeric.

@javiereguiluz
Copy link
Member Author

So $requestValue is the value selected by the user? (if true: $selectedValue maybe?)

@vudaltsov
Copy link
Contributor

@javiereguiluz , I gave a wrong explanation. The third argument is not the selected value. This callback will be called for all choices. So we are not talking about user here - the submit has not happened yet. The third argument is how the choice is represented in a view in <option value="X">.

@vudaltsov
Copy link
Contributor

What about this: function ($choiceData, $choiceKey, $viewValue)

@yceruto
Copy link
Member

yceruto commented May 29, 2018

or... function ($choiceValue, $choiceKey, $viewValue) ?

@vudaltsov
Copy link
Contributor

The problem with $choiceValue is that we have an option choice_value which is about $viewValue and that might be misleading. While data indicates that this is more of a model thing, the actual choice data.

@yceruto
Copy link
Member

yceruto commented May 29, 2018

Well, I think it's better explain each argument below the first example so.

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

Successfully merging this pull request may close these issues.

4 participants