Skip to content

[Autocomplete] Fix bug of not using choice_value #1328

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 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
use Symfony\Component\Form\ChoiceList\Factory\Cache;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -89,7 +89,7 @@ public function getLabel(object $entity): string
return $this->propertyAccessor->getValue($entity, $choiceLabel);
}

if ($choiceLabel instanceof ChoiceLabel) {
if ($choiceLabel instanceof Cache\ChoiceLabel) {
Copy link
Member

Choose a reason for hiding this comment

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

A particular reason for this change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To have only one use.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this offers a better readability, but let's see what others think

Copy link
Contributor Author

@ytilotti ytilotti Dec 14, 2023

Choose a reason for hiding this comment

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

I'm not sure this offers a better readability, but let's see what others think

Used in https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php.
I have no personal opinion, whatever ;)

$choiceLabel = $choiceLabel->getOption();
}

Expand All @@ -99,7 +99,17 @@ public function getLabel(object $entity): string

public function getValue(object $entity): string
{
return $this->getEntityMetadata()->getIdValue($entity);
$choiceValue = $this->getFormOption('choice_value');

if (\is_string($choiceValue) || $choiceValue instanceof PropertyPathInterface) {
return $this->propertyAccessor->getValue($entity, $choiceValue);
Copy link
Member

Choose a reason for hiding this comment

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

The return type of the method is not compatible with all the new possibilities no ? Integers, floats, nulls, ...

Copy link
Contributor Author

@ytilotti ytilotti Dec 14, 2023

Choose a reason for hiding this comment

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

Historically, there's already a "problem" when we see the return type of getIdentifierValues():

/**
     * Returns the identifier of this object as an array with field name as key.
     *
     * Has to return an empty array if no identifier isset.
     *
     * @return array<string, mixed>
     */
    public function getIdentifierValues(object $object);

But there is no problem because is always cast to string.

By default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible. For example, a function that is given an int for a parameter that expects a string will get a variable of type string.

}

if ($choiceValue instanceof Cache\ChoiceValue) {
$choiceValue = $choiceValue->getOption();
}

return $choiceValue($entity);
}

public function isGranted(Security $security): bool
Expand Down