Skip to content

Commit aac29cb

Browse files
bug #520 Autocomplete choice label (weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- Autocomplete choice label | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | none | License | MIT If your autocomplete field does not specify `choice_label`, this creates a choice label situation that we apparently weren't handling yet: <img width="973" alt="Screen Shot 2022-11-03 at 10 16 58 AM" src="https://user-images.githubusercontent.com/121003/199744820-10196553-21dc-4a01-a134-9133aa8cea4f.png"> Fixed with test. Commits ------- 7dd8c9d Fixing a bug where noi choice_label + autocomplete caused an error
2 parents 00a34e5 + dc34a0b commit aac29cb

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\ORM\EntityRepository;
66
use Doctrine\ORM\QueryBuilder;
7+
use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
78
use Symfony\Component\Form\FormFactoryInterface;
89
use Symfony\Component\Form\FormInterface;
910
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -79,6 +80,10 @@ public function getLabel(object $entity): string
7980
return $this->propertyAccessor->getValue($entity, $choiceLabel);
8081
}
8182

83+
if ($choiceLabel instanceof ChoiceLabel) {
84+
$choiceLabel = $choiceLabel->getOption();
85+
}
86+
8287
// 0 hardcoded as the "index", should not be relevant
8388
return $choiceLabel($entity, 0, $this->getValue($entity));
8489
}

src/Autocomplete/tests/Fixtures/Entity/Category.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public function removeProduct(Product $product): self
7272

7373
return $this;
7474
}
75+
76+
public function __toString(): string
77+
{
78+
return $this->getName();
79+
}
7580
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Form;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\HttpFoundation\RequestStack;
9+
use Symfony\Component\OptionsResolver\OptionsResolver;
10+
use Symfony\Component\Security\Core\Security;
11+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
12+
use Symfony\UX\Autocomplete\Form\ParentEntityAutocompleteType;
13+
14+
#[AsEntityAutocompleteField]
15+
class CategoryNoChoiceLabelAutocompleteType extends AbstractType
16+
{
17+
public function configureOptions(OptionsResolver $resolver)
18+
{
19+
$resolver->setDefaults([
20+
'class' => Category::class,
21+
'placeholder' => 'What should we eat?',
22+
]);
23+
}
24+
25+
public function getParent(): string
26+
{
27+
return ParentEntityAutocompleteType::class;
28+
}
29+
}

src/Autocomplete/tests/Functional/FieldAutocompleterTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Zenstruck\Foundry\Test\Factories;
1919
use Zenstruck\Foundry\Test\ResetDatabase;
2020

21-
// tests CategoryAutocompleteType
2221
class FieldAutocompleterTest extends KernelTestCase
2322
{
2423
use Factories;
@@ -90,4 +89,16 @@ public function testItCheckMaxResultsOption(): void
9089
->assertJsonMatches('length(results)', 5)
9190
;
9291
}
92+
93+
public function testItWorksWithoutAChoiceLabel(): void
94+
{
95+
CategoryFactory::createMany(5, ['name' => 'foo']);
96+
97+
$this->browser()
98+
->throwExceptions()
99+
->get('/test/autocomplete/category_no_choice_label_autocomplete_type?query=foo')
100+
->assertSuccessful()
101+
->assertJsonMatches('length(results)', 5)
102+
;
103+
}
93104
}

0 commit comments

Comments
 (0)