Hello! In this article I would like to describe how we can add custom validation
for our custom entity type.
Let’s use a ´our_custom_module_name´ module, as example, to create an entity validation,
which will check if we have the same entity already (with the same title).
Inside the module folder we should create a constraint:
Constraints in Drupal 8 are also plugins which usually hold a small amount of information about how data is actually being validated, what error message should be used in case of failure and any additional options the validator needs.
/our_custom_module_name/src/Plugin/Validation/Constraint/ExistsEntityWithNameConstraint.php
|
|
Next, we should create a validator class, where all our validation logic should be placed:
The validator class (which is referenced by the constraint) is responsible for checking the data.
/our_custom_module_name/src/Plugin/Validation/Constraint/ExistsEntityWithNameConstraintValidator.php
|
|
And last step. We should add annotation to our custom entity to use this validation:
|
|