Skip to content

Commit fcf87d9

Browse files
author
Kai Eichinger
committed
Add validation constraint @ValidEmbeddable()
1 parent 99a305f commit fcf87d9

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
8.5.0
2+
=====
3+
4+
* (feature) Add validation constraint `@ValidEmbeddable()`, which works exactly like `@Asserts\Valid()` but allows you to set `embeddedGroups()`,
5+
which will be passed down to the validator when validating the embeddable.
6+
7+
18
8.4.2
29
=====
310

src/Validator/ValidEmbeddable.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Becklyn\Rad\Validator;
4+
5+
use Symfony\Component\Validator\Constraints\Valid;
6+
7+
/**
8+
* @Annotation()
9+
*/
10+
class ValidEmbeddable extends Valid
11+
{
12+
public array $embeddedGroups = ["Default"];
13+
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
public function getTargets () : array
19+
{
20+
return [
21+
self::PROPERTY_CONSTRAINT,
22+
];
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Becklyn\Rad\Validator;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
use Symfony\Component\Validator\ConstraintValidator;
7+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8+
9+
class ValidEmbeddableValidator extends ConstraintValidator
10+
{
11+
public function validate($value, Constraint $constraint) : void
12+
{
13+
if (!$constraint instanceof ValidEmbeddable)
14+
{
15+
throw new UnexpectedTypeException($constraint, ValidEmbeddable::class);
16+
}
17+
18+
if (null === $value)
19+
{
20+
return;
21+
}
22+
23+
$this->context
24+
->getValidator()
25+
->inContext($this->context)
26+
->validate($value, null, $constraint->embeddedGroups);
27+
}
28+
}

0 commit comments

Comments
 (0)