EntityTestCompositeConstraintValidator.php in Drupal 8
File
core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraintValidator.php
View source
<?php
namespace Drupal\entity_test\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class EntityTestCompositeConstraintValidator extends ConstraintValidator {
protected $context;
public function validate($entity, Constraint $constraint) {
if ($entity->name->value === 'test' && $entity->type->value === 'test2') {
$this->context
->buildViolation($constraint->message)
->atPath('type')
->addViolation();
}
if ($entity->name->value === 'failure-field-name') {
$this->context
->buildViolation('Name field violation')
->atPath('name')
->addViolation();
}
elseif ($entity->name->value === 'failure-field-type') {
$this->context
->buildViolation('Type field violation')
->atPath('type')
->addViolation();
}
}
}