Skip to content

Commit 85629ce

Browse files
committed
bug #1185 Fix ExtraLazyChoiceLoader (yceruto)
This PR was merged into the 2.x branch. Discussion ---------- Fix ExtraLazyChoiceLoader | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | - | License | MIT Currently, when default data is provided through the autocomplete field, it is not validated against the database or custom query builder. These changes aim to fix that issue and also simplify the code, while the goal remains the same. Commits ------- b35fda3 Fix ExtraLazyChoiceLoader
2 parents 340edb7 + b35fda3 commit 85629ce

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

src/Autocomplete/src/Form/ChoiceList/Loader/ExtraLazyChoiceLoader.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
class ExtraLazyChoiceLoader implements ChoiceLoaderInterface
2222
{
2323
private ?ChoiceListInterface $choiceList = null;
24-
private array $choices = [];
25-
private bool $cached = false;
2624

2725
public function __construct(
2826
private readonly ChoiceLoaderInterface $decorated,
@@ -31,38 +29,21 @@ public function __construct(
3129

3230
public function loadChoiceList(callable $value = null): ChoiceListInterface
3331
{
34-
if (null !== $this->choiceList && $this->cached) {
35-
return $this->choiceList;
36-
}
37-
38-
$this->cached = true;
39-
40-
return $this->choiceList = new ArrayChoiceList($this->choices, $value);
32+
return $this->choiceList ??= new ArrayChoiceList([], $value);
4133
}
4234

4335
public function loadChoicesForValues(array $values, callable $value = null): array
4436
{
45-
if ($this->choices !== $choices = $this->decorated->loadChoicesForValues($values, $value)) {
46-
$this->cached = false;
47-
}
37+
$choices = $this->decorated->loadChoicesForValues($values, $value);
38+
$this->choiceList = new ArrayChoiceList($choices, $value);
4839

49-
return $this->choices = $choices;
40+
return $choices;
5041
}
5142

5243
public function loadValuesForChoices(array $choices, callable $value = null): array
5344
{
5445
$values = $this->decorated->loadValuesForChoices($choices, $value);
55-
56-
if ([] === $values || [''] === $values) {
57-
$newChoices = [];
58-
} else {
59-
$newChoices = $choices;
60-
}
61-
62-
if ($this->choices !== $newChoices) {
63-
$this->choices = $newChoices;
64-
$this->cached = false;
65-
}
46+
$this->loadChoicesForValues($values, $value);
6647

6748
return $values;
6849
}

0 commit comments

Comments
 (0)