EntityTestCompositeConstraintValidator.php

Same filename in other branches
  1. 8.9.x core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraintValidator.php
  2. 10 core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraintValidator.php
  3. 11.x core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraintValidator.php

Namespace

Drupal\entity_test\Plugin\Validation\Constraint

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;

/**
 * Constraint validator for the EntityTestComposite constraint.
 */
class EntityTestCompositeConstraintValidator extends ConstraintValidator {
    
    /**
     * Validator 2.5 and upwards compatible execution context.
     *
     * @var \Symfony\Component\Validator\Context\ExecutionContextInterface
     */
    protected $context;
    
    /**
     * {@inheritdoc}
     */
    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();
        }
    }

}

Classes

Title Deprecated Summary
EntityTestCompositeConstraintValidator Constraint validator for the EntityTestComposite constraint.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.