Skip to content

Commit b341535

Browse files
xabbuhHypeMC
authored andcommitted
deal with fields for which no constraints have been configured
1 parent fa7531f commit b341535

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-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+
if ([] === $options) {
98+
return true;
99+
}
100+
101+
foreach ($options as $optionOrField) {
102+
if ($optionOrField instanceof Constraint) {
103+
return true;
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,41 @@ 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+
public function testEmptyConstraintListFor()
179+
{
180+
$constraint = new Collection([
181+
'foo' => [],
182+
],
183+
null,
184+
null,
185+
true,
186+
null,
187+
'foo bar baz'
188+
);
189+
190+
$this->assertArrayHasKey('foo', $constraint->fields);
191+
$this->assertInstanceOf(Required::class, $constraint->fields['foo']);
192+
$this->assertTrue($constraint->allowExtraFields);
193+
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
194+
}
195+
196+
public function testEmptyConstraintListForFieldInOptions()
197+
{
198+
$constraint = new Collection([
199+
'fields' => [
200+
'foo' => [],
201+
],
202+
'allowExtraFields' => true,
203+
'extraFieldsMessage' => 'foo bar baz',
204+
]);
205+
206+
$this->assertArrayHasKey('foo', $constraint->fields);
207+
$this->assertInstanceOf(Required::class, $constraint->fields['foo']);
173208
$this->assertTrue($constraint->allowExtraFields);
174209
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
175210
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy