3 namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
5 use Drupal\Core\TypedData\ComplexDataInterface;
6 use Drupal\Core\TypedData\TypedDataInterface;
7 use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
13 * Validates complex data.
15 class ComplexDataConstraintValidator extends ConstraintValidator {
17 use TypedDataAwareValidatorTrait;
22 public function validate($data, Constraint $constraint) {
24 // If un-wrapped data has been passed, fetch the typed data object first.
25 if (!$data instanceof TypedDataInterface) {
26 $data = $this->getTypedData();
28 if (!$data instanceof ComplexDataInterface) {
29 throw new UnexpectedTypeException($data, 'ComplexData');
32 foreach ($constraint->properties as $name => $constraints) {
33 $this->context->getValidator()
34 ->inContext($this->context)
35 // Specifically pass along FALSE as $root_call, as we validate the data
36 // as part of the typed data tree.
37 ->validate($data->get($name), $constraints, NULL, FALSE);