1<?php 2 3namespace Drupal\entity_test\Plugin\Validation\Constraint; 4 5use Symfony\Component\Validator\Constraint; 6use Symfony\Component\Validator\ConstraintValidator; 7 8/** 9 * Constraint validator for the EntityTestComposite constraint. 10 */ 11class EntityTestCompositeConstraintValidator extends ConstraintValidator { 12 13 /** 14 * Validator 2.5 and upwards compatible execution context. 15 * 16 * @var \Symfony\Component\Validator\Context\ExecutionContextInterface 17 */ 18 protected $context; 19 20 /** 21 * {@inheritdoc} 22 */ 23 public function validate($entity, Constraint $constraint) { 24 25 if ($entity->name->value === 'test' && $entity->type->value === 'test2') { 26 $this->context->buildViolation($constraint->message) 27 ->atPath('type') 28 ->addViolation(); 29 } 30 if ($entity->name->value === 'failure-field-name') { 31 $this->context->buildViolation('Name field violation') 32 ->atPath('name') 33 ->addViolation(); 34 } 35 elseif ($entity->name->value === 'failure-field-type') { 36 $this->context->buildViolation('Type field violation') 37 ->atPath('type') 38 ->addViolation(); 39 } 40 } 41 42} 43