From 0bf5209379a1b14427c06a27c4791aa46a6cde17 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 5 Jan 2024 16:42:05 +0100 Subject: [PATCH] deal with fields for which no constraints have been configured --- .../Validator/Constraints/Collection.php | 29 ++++++++++++++- .../Tests/Constraints/CollectionTest.php | 35 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index 1d0c69ffa74db..8d66141b4d8d7 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -42,7 +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) && ([] === $fields || ($firstField = reset($fields)) instanceof Constraint || ($firstField[0] ?? null) instanceof Constraint)) { + if (self::isFieldsOption($fields)) { $fields = ['fields' => $fields]; } @@ -87,4 +87,31 @@ protected function getCompositeOption() { return 'fields'; } + + private static function isFieldsOption($options): bool + { + if (!\is_array($options)) { + return false; + } + + if ([] === $options) { + return true; + } + + foreach ($options as $optionOrField) { + if ($optionOrField instanceof Constraint) { + return true; + } + + if (!\is_array($optionOrField)) { + return false; + } + + if ([] !== $optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) { + return false; + } + } + + return true; + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php index 2b9acb8d43fa4..4ff8b6d6aac6a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php @@ -170,6 +170,41 @@ public function testEmptyFieldsInOptions() 'extraFieldsMessage' => 'foo bar baz', ]); + $this->assertSame([], $constraint->fields); + $this->assertTrue($constraint->allowExtraFields); + $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); + } + + public function testEmptyConstraintListFor() + { + $constraint = new Collection([ + 'foo' => [], + ], + null, + null, + true, + null, + 'foo bar baz' + ); + + $this->assertArrayHasKey('foo', $constraint->fields); + $this->assertInstanceOf(Required::class, $constraint->fields['foo']); + $this->assertTrue($constraint->allowExtraFields); + $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); + } + + public function testEmptyConstraintListForFieldInOptions() + { + $constraint = new Collection([ + 'fields' => [ + 'foo' => [], + ], + 'allowExtraFields' => true, + 'extraFieldsMessage' => 'foo bar baz', + ]); + + $this->assertArrayHasKey('foo', $constraint->fields); + $this->assertInstanceOf(Required::class, $constraint->fields['foo']); $this->assertTrue($constraint->allowExtraFields); $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); }