Skip to content

Commit

Permalink
[Validator] Fix fields without constraints in Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Feb 7, 2024
1 parent 89b3d63 commit 591b4d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Expand Up @@ -94,20 +94,20 @@ private static function isFieldsOption($options): bool
return false;
}

if ([] === $options) {
return true;
}

foreach ($options as $optionOrField) {
if ($optionOrField instanceof Constraint) {
return true;
}

if (null === $optionOrField) {
return true;
}

if (!\is_array($optionOrField)) {
return false;
}

if ([] !== $optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
if ($optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
return false;
}
}
Expand Down
Expand Up @@ -175,10 +175,15 @@ public function testEmptyFieldsInOptions()
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
}

public function testEmptyConstraintListFor()
/**
* @testWith [[]]
* [null]
*/
public function testEmptyConstraintListForField(?array $fieldConstraint)
{
$constraint = new Collection([
'foo' => [],
$constraint = new Collection(
[
'foo' => $fieldConstraint,
],
null,
null,
Expand All @@ -193,11 +198,15 @@ public function testEmptyConstraintListFor()
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
}

public function testEmptyConstraintListForFieldInOptions()
/**
* @testWith [[]]
* [null]
*/
public function testEmptyConstraintListForFieldInOptions(?array $fieldConstraint)
{
$constraint = new Collection([
'fields' => [
'foo' => [],
'foo' => $fieldConstraint,
],
'allowExtraFields' => true,
'extraFieldsMessage' => 'foo bar baz',
Expand Down

0 comments on commit 591b4d1

Please sign in to comment.