8000 [Validator] Add the `exclude` option to the `Cascade` constraint by alexandre-daubois · Pull Request #49596 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add the exclude option to the Cascade constraint #49596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add a `NoSuspiciousCharacters` constraint to validate a string is not a spoofing attempt
* 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`)
* Add the `filenameMaxLength` option to the `File` constraint
* Add the `exclude` option to the `Cascade` constraint

6.2
---
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@
#[\Attribute(\Attribute::TARGET_CLASS)]
class Cascade extends Constraint
{
public function __construct(array $options = null)
public array $exclude = [];

public function __construct(array|string|null $exclude = null, array $options = null)
{
if (\is_array($exclude) && !array_is_list($exclude)) {
$options = array_merge($exclude, $options);
} else {
$this->exclude = array_flip((array) $exclude);
}

if (\is_array($options) && \array_key_exists('groups', $options)) {
throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public function addConstraint(Constraint $constraint): static
$this->cascadingStrategy = CascadingStrategy::CASCADE;

foreach ($this->getReflectionClass()->getProperties() as $property) {
if (isset($constraint->exclude[$property->getName()])) {
continue;
}

if ($property->hasType() && (('array' === $type = $property->getType()->getName()) || class_exists($type))) {
$this->addPropertyConstraint($property->getName(), new Valid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ public function testCascadeConstraint()
'children',
], $metadata->getConstrainedProperties());
}

public function testCascadeConstraintWithExcludedProperties()
{
$metadata = new ClassMetadata(CascadingEntity::class);

$metadata->addConstraint(new Cascade(exclude: ['requiredChild', 'optionalChild']));

$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
$this->assertCount(2, $metadata->properties);
$this->assertSame([
'staticChild',
'children',
], $metadata->getConstrainedProperties());
}
}

class ClassCompositeConstraint extends Composite
Expand Down
0