Skip to content

Commit 15a3bf6

Browse files
committed
[Autocomplete] Don't add WHERE IN criteria without params
1 parent 843f1dd commit 15a3bf6

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Autocomplete/src/Form/AutocompleteEntityTypeSubscriber.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ public function preSubmit(FormEvent $event)
7373
++$idx;
7474
}
7575

76-
$options['choices'] = $repository->createQueryBuilder('o')
77-
->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
78-
->setParameters(new ArrayCollection($params))
79-
->getQuery()
80-
->getResult();
76+
$queryBuilder = $repository->createQueryBuilder('o');
77+
78+
if ($params) {
79+
$queryBuilder
80+
->where(sprintf("o.$idField IN (%s)", implode(', ', array_keys($params))))
81+
->setParameters(new ArrayCollection($params));
82+
}
83+
84+
$options['choices'] = $queryBuilder->getQuery()->getResult();
8185
} else {
8286
$options['choices'] = $repository->createQueryBuilder('o')
8387
->where("o.$idField = :id")

src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,25 @@ public function testProperlyLoadsChoicesWithIdValueObjects()
9393
->assertContains('Sugar')
9494
;
9595
}
96+
97+
public function testMultipleDoesNotFailWithoutSelectedChoices()
98+
{
99+
$this->browser()
100+
->throwExceptions()
101+
->get('/test-form')
102+
->assertElementCount('#product_ingredients_autocomplete option', 0)
103+
->assertNotContains('Flour')
104+
->assertNotContains('Sugar')
105+
->post('/test-form', [
106+
'body' => [
107+
'product' => [
108+
'ingredients' => [
109+
'autocomplete' => [],
110+
],
111+
],
112+
],
113+
])
114+
->assertElementCount('#product_ingredients_autocomplete option', 0)
115+
;
116+
}
96117
}

0 commit comments

Comments
 (0)