Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d344aa

Browse files
bug #53755 [Validator] Fix fields without constraints in Collection (xabbuh, HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] Fix fields without constraints in `Collection` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #53133 (comment), Fix #53455 | License | MIT Continuation of #53443. Commits ------- f6217d8 [Validator] Fix fields without constraints in `Collection` b341535 deal with fields for which no constraints have been configured
2 parents 3870d1e + f6217d8 commit 6d344aa

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/Symfony/Component/Validator/Constraints/Collection.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Collection extends Composite
4242
*/
4343
public function __construct($fields = null, ?array $groups = null, $payload = null, ?bool $allowExtraFields = null, ?bool $allowMissingFields = null, ?string $extraFieldsMessage = null, ?string $missingFieldsMessage = null)
4444
{
45-
if (\is_array($fields) && ([] === $fields || ($firstField = reset($fields)) instanceof Constraint || ($firstField[0] ?? null) instanceof Constraint)) {
45+
if (self::isFieldsOption($fields)) {
4646
$fields = ['fields' => $fields];
4747
}
4848

@@ -87,4 +87,31 @@ protected function getCompositeOption()
8787
{
8888
return 'fields';
8989
}
90+
91+
private static function isFieldsOption($options): bool
92+
{
93+
if (!\is_array($options)) {
94+
return false;
95+
}
96+
97+
foreach ($options as $optionOrField) {
98+
if ($optionOrField instanceof Constraint) {
99+
return true;
100+
}
101+
102+
if (null === $optionOrField) {
103+
continue;
104+
}
105+
106+
if (!\is_array($optionOrField)) {
107+
return false;
108+
}
109+
110+
if ($optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
111+
return false;
112+
}
113+
}
114+
115+
return true;
116+
}
90117
}

src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,50 @@ public function testEmptyFieldsInOptions()
170170
'extraFieldsMessage' => 'foo bar baz',
171171
]);
172172

173+
$this->assertSame([], $constraint->fields);
174+
$this->assertTrue($constraint->allowExtraFields);
175+
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
176+
}
177+
178+
/**
179+
* @testWith [[]]
180+
* [null]
181+
*/
182+
public function testEmptyConstraintListForField(?array $fieldConstraint)
183+
{
184+
$constraint = new Collection(
185+
[
186+
'foo' => $fieldConstraint,
187+
],
188+
null,
189+
null,
190+
true,
191+
null,
192+
'foo bar baz'
193+
);
194+
195+
$this->assertArrayHasKey('foo', $constraint->fields);
196+
$this->assertInstanceOf(Required::class, $constraint->fields['foo']);
197+
$this->assertTrue($constraint->allowExtraFields);
198+
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
199+
}
200+
201+
/**
202+
* @testWith [[]]
203+
* [null]
204+
*/
205+
public function testEmptyConstraintListForFieldInOptions(?array $fieldConstraint)
206+
{
207+
$constraint = new Collection([
208+
'fields' => [
209+
'foo' => $fieldConstraint,
210+
],
211+
'allowExtraFields' => true,
212+
'extraFieldsMessage' => 'foo bar baz',
213+
]);
214+
215+
$this->assertArrayHasKey('foo', $constraint->fields);
216+
$this->assertInstanceOf(Required::class, $constraint->fields['foo']);
173217
$this->assertTrue($constraint->allowExtraFields);
174218
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
175219
}

0 commit comments

Comments
 (0)