diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index ac35a27d052c7..27ba4613f864c 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -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; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php index 4ff8b6d6aac6a..19cffc693158f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php @@ -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, @@ -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',