Introduction to Drupal 8 Entity Validation

Joeri Poesen //

Entity validation refers to specifying criteria (constraints) which must be met before an entity can be saved (created or updated).

A traditional way to do this in Drupal has been to implement hook_form_alter() and add a custom form validation handler to the entity creation form — usually the node edit form.

Custom validation handlers are a perfectly good option, but their drawback is that your custom validation will only happen when entities are created via the entity creation form; validation will not happen when entities are created/updated via any other mechanism: data migration, creation via REST or JSON API web service, custom code snippets, ...

To ensure custom entity validation happens regardless of the method of creation, use the Entity Validation API.

Head over to https://blog.codeenigma.com/introduction-to-drupal-8-entity-validation-f0aa37c1f9a3 to read my full article.