From 9de3680da037655a6dc409c9378166bb3bdc3a13 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 4 Feb 2024 17:24:55 +0100 Subject: [PATCH] [Validator] Fix fields without constraints in `Collection` --- .../Validator/Constraints/Collection.php | 4 ++++ .../Tests/Constraints/CollectionTest.php | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index ac35a27d052c7..5d740a0af6384 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -103,6 +103,10 @@ private static function isFieldsOption($options): bool return true; } + if (null === $optionOrField) { + return true; + } + if (!\is_array($optionOrField)) { 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',