Skip to content

Commit

Permalink
bug #53383 [Validator] re-allow an empty list of fields (xabbuh)
Browse files Browse the repository at this point in the history
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 #53133 (comment)
| License       | MIT

Commits
-------

098c14c re-allow an empty list of fields
  • Loading branch information
nicolas-grekas committed Jan 5, 2024
2 parents 3e6780b + 098c14c commit 218313e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ class Collection extends Composite
*/
public function __construct($fields = null, array $groups = null, $payload = null, bool $allowExtraFields = null, bool $allowMissingFields = null, string $extraFieldsMessage = null, string $missingFieldsMessage = null)
{
if (\is_array($fields)
&& (($firstField = reset($fields)) instanceof Constraint
|| ($firstField[0] ?? null) instanceof Constraint
)) {
if (\is_array($fields) && ([] === $fields || ($firstField = reset($fields)) instanceof Constraint || ($firstField[0] ?? null) instanceof Constraint)) {
$fields = ['fields' => $fields];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,24 @@ public function testAllKeysAreKnowOptions()
$this->assertTrue($constraint->allowExtraFields);
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
}

public function testEmptyFields()
{
$constraint = new Collection([], [], null, true, null, 'foo bar baz');

$this->assertTrue($constraint->allowExtraFields);
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
}

public function testEmptyFieldsInOptions()
{
$constraint = new Collection([
'fields' => [],
'allowExtraFields' => true,
'extraFieldsMessage' => 'foo bar baz',
]);

$this->assertTrue($constraint->allowExtraFields);
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
}
}

0 comments on commit 218313e

Please sign in to comment.