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

Skip to content

Commit a699033

Browse files
[Validator] Add the excluded option to the Cascade constraint
1 parent 21c8789 commit a699033

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Add a `NoSuspiciousCharacters` constraint to validate a string is not a spoofing attempt
1111
* Add the `countUnit` option to the `Length` constraint to allow counting the string length either by code points (like before, now the default setting `Length::COUNT_CODEPOINTS`), bytes (`Length::COUNT_BYTES`) or graphemes (`Length::COUNT_GRAPHEMES`)
1212
* Add the `filenameMaxLength` option to the `File` constraint
13+
* Add the `excluded` option to the `Cascade` constraint
1314

1415
6.2
1516
---

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@
2323
#[\Attribute(\Attribute::TARGET_CLASS)]
2424
class Cascade extends Constraint
2525
{
26-
public function __construct(array $options = null)
26+
public array $excluded = [];
27+
28+
public function __construct(array|string|null $excluded = null, array $options = null)
2729
{
2830
if (\is_array($options) && \array_key_exists('groups', $options)) {
2931
throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__));
3032
}
3133

34+
if (\is_array($excluded) && \is_string(key($excluded))) {
35+
$options = array_merge($excluded, $options);
36+
} else {
37+
$this->excluded = (array) $excluded;
38+
}
39+
3240
parent::__construct($options);
3341
}
3442

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ public function addConstraint(Constraint $constraint): static
194194
$this->cascadingStrategy = CascadingStrategy::CASCADE;
195195

196196
foreach ($this->getReflectionClass()->getProperties() as $property) {
197+
if (\in_array($property->getName(), $constraint->excluded)) {
198+
continue;
199+
}
200+
197201
if ($property->hasType() && (('array' === $type = $property->getType()->getName()) || class_exists($type))) {
198202
$this->addPropertyConstraint($property->getName(), new Valid());
199203
}

src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,20 @@ public function testCascadeConstraint()
351351
'children',
352352
], $metadata->getConstrainedProperties());
353353
}
354+
355+
public function testCascadeConstraintWithExcludedProperties()
356+
{
357+
$metadata = new ClassMetadata(CascadingEntity::class);
358+
359+
$metadata->addConstraint(new Cascade(excluded: ['requiredChild', 'optionalChild']));
360+
361+
$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
362+
$this->assertCount(2, $metadata->properties);
363+
$this->assertSame([
364+
'staticChild',
365+
'children',
366+
], $metadata->getConstrainedProperties());
367+
}
354368
}
355369

356370
class ClassCompositeConstraint extends Composite

0 commit comments

Comments
 (0)