Skip to content

Commit 218313e

Browse files
bug symfony#53383 [Validator] re-allow an empty list of fields (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] re-allow an empty list of fields | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#53133 (comment) | License | MIT Commits ------- 098c14c re-allow an empty list of fields
2 parents 3e6780b + 098c14c commit 218313e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Symfony/Component/Validator/Constraints/Collection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ class Collection extends Composite
4242
*/
4343
public function __construct($fields = null, array $groups = null, $payload = null, bool $allowExtraFields = null, bool $allowMissingFields = null, string $extraFieldsMessage = null, string $missingFieldsMessage = null)
4444
{
45-
if (\is_array($fields)
46-
&& (($firstField = reset($fields)) instanceof Constraint
47-
|| ($firstField[0] ?? null) instanceof Constraint
48-
)) {
45+
if (\is_array($fields) && ([] === $fields || ($firstField = reset($fields)) instanceof Constraint || ($firstField[0] ?? null) instanceof Constraint)) {
4946
$fields = ['fields' => $fields];
5047
}
5148

src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,24 @@ public function testAllKeysAreKnowOptions()
153153
$this->assertTrue($constraint->allowExtraFields);
154154
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
155155
}
156+
157+
public function testEmptyFields()
158+
{
159+
$constraint = new Collection([], [], null, true, null, 'foo bar baz');
160+
161+
$this->assertTrue($constraint->allowExtraFields);
162+
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
163+
}
164+
165+
public function testEmptyFieldsInOptions()
166+
{
167+
$constraint = new Collection([
168+
'fields' => [],
169+
'allowExtraFields' => true,
170+
'extraFieldsMessage' => 'foo bar baz',
171+
]);
172+
173+
$this->assertTrue($constraint->allowExtraFields);
174+
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
175+
}
156176
}

0 commit comments

Comments
 (0)