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

Skip to content

Commit fef6e80

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

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@
1616

1717
/**
1818
* @Annotation
19+
*
1920
* @Target({"CLASS"})
2021
*
2122
* @author Jules Pietri <[email protected]>
2223
*/
2324
#[\Attribute(\Attribute::TARGET_CLASS)]
2425
class Cascade extends Constraint
2526
{
26-
public function __construct(array $options = null)
27+
public array $excluded = [];
28+
29+
public function __construct(array|string|null $excluded = null, array $options = null)
2730
{
2831
if (\is_array($options) && \array_key_exists('groups', $options)) {
2932
throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__));
3033
}
3134

35+
if (\is_array($excluded) && \is_string(key($excluded))) {
36+
$options = array_merge($excluded, $options);
37+
} else {
38+
$this->excluded = (array) $excluded;
39+
}
40+
3241
parent::__construct($options);
3342
}
3443

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)