Skip to content

Instantly share code, notes, and snippets.

@jerbob92
Created August 27, 2016 11:00
Show Gist options
  • Save jerbob92/8b86d1b24e9325d856da1fed9a5bf6d0 to your computer and use it in GitHub Desktop.
Save jerbob92/8b86d1b24e9325d856da1fed9a5bf6d0 to your computer and use it in GitHub Desktop.
Validate Entity D8
<?php
$errors = array();
$validate = $entity->validate();
$violations = $validate->getIterator();
foreach ($violations as $violation) {
$constraint = get_class($violation->getConstraint());
$type = $constraint;
$msg = $violation->getMessageTemplate();
if ($constraint == 'Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraint') {
$type = 'required';
$msg = t('This field is required');
}
$path = $violation->getPropertyPath();
$path = explode('.', $path);
/* @var $violation \Symfony\Component\Validator\ConstraintViolation */
$errors[] = array(
'field' => $path[0],
'type' => $type,
'msg' => $msg,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment