Skip to content

Autocomplete choice label #520

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

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -79,6 +80,10 @@ public function getLabel(object $entity): string
return $this->propertyAccessor->getValue($entity, $choiceLabel);
}

if ($choiceLabel instanceof ChoiceLabel) {
$choiceLabel = $choiceLabel->getOption();
}

// 0 hardcoded as the "index", should not be relevant
return $choiceLabel($entity, 0, $this->getValue($entity));
}
Expand Down
5 changes: 5 additions & 0 deletions src/Autocomplete/tests/Fixtures/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ public function removeProduct(Product $product): self

return $this;
}

public function __toString(): string
{
return $this->getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\UX\Autocomplete\Tests\Fixtures\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\ParentEntityAutocompleteType;

#[AsEntityAutocompleteField]
class CategoryNoChoiceLabelAutocompleteType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => Category::class,
'placeholder' => 'What should we eat?',
]);
}

public function getParent(): string
{
return ParentEntityAutocompleteType::class;
}
}
13 changes: 12 additions & 1 deletion src/Autocomplete/tests/Functional/FieldAutocompleterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

// tests CategoryAutocompleteType
class FieldAutocompleterTest extends KernelTestCase
{
use Factories;
Expand Down Expand Up @@ -90,4 +89,16 @@ public function testItCheckMaxResultsOption(): void
->assertJsonMatches('length(results)', 5)
;
}

public function testItWorksWithoutAChoiceLabel(): void
{
CategoryFactory::createMany(5, ['name' => 'foo']);

$this->browser()
->throwExceptions()
->get('/test/autocomplete/category_no_choice_label_autocomplete_type?query=foo')
->assertSuccessful()
->assertJsonMatches('length(results)', 5)
;
}
}