-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] ObjectChoiceList should select initial field value based on the value property. #8825
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
Thank you! :) I will look into this ASAP. |
I tried to have a look yesterday, but I didn’t really understand what I was doing. It seems to come from the I first tried overriding |
A side effect of the same issue is that the objects given in |
I took some effort on this issue. It seemed to me that the problem is the comparison for equality in ChoiceList. Overriding the public function getValuesForChoices(array $choices)
{
$choices = $this->fixChoices($choices);
$values = array();
$availableValues = $this->getValues();
foreach ($this->getChoices() as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
if ($this->createValue($choice) === $this->createValue($givenChoice)) {
$values[] = $availableValues[$i];
unset($choices[$j]);
if (0 === count($choices)) {
break 2;
}
}
}
}
return $values;
} But I am not so familiar with the internals of the Form component. Thus, I'm not quite sure if this is a general solution. What do you think, @bschussek? |
@bschussek should I create a pull request for this? |
👍, same issue here, I agree that, if, at least, a valuePath is given, it should compare on the actual value instead of comparing the (reference of) the objects. |
+1, I am having the same issue. I hope this pull request passes and gets pulled in. |
…value, if a value path is given (webmozart) This PR was merged into the 2.5-dev branch. Discussion ---------- [Form] ObjectChoiceList now compares choices by their value, if a value path is given | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8825 | License | MIT | Doc PR | - Commits ------- ce0efb1 [Form] ObjectChoiceList now compares choices by their value, if a value path is given
When using an
ObjectChoiceList
with a value path given, and presetting the form data with some initial value, it only works if the object in the form data is the same instance as the corresponding object in the choice list.I would expect the form data and the choices to be matched by their value (the value of the property at
$valuePath
) instead. Indeed, this is what happens when the form is submitted: the request contains the value and Symfony uses it to retrieve the matching object from the choices.Here is a fork of
symfony-standard
that demonstrates this. The demo is located at/demo/objectchoicelist
.https://github.com/oscherler/symfony-standard/tree/objectchoicelist
In the first 4 fields (Same instance …) I use an object from the choices to populate the form data, and the value is properly selected.
In the next 4 fields (Other instance …) I create a new object with the same properties (same value) as one of the objects in the choice list, and no value is selected.
The text was updated successfully, but these errors were encountered: