You are here

protected function EntityResource::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::validate()

Verifies that the whole entity does not violate any validation constraints.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException If validation errors are found.

2 calls to EntityResource::validate()
EntityResource::patch in core/modules/rest/src/Plugin/rest/resource/EntityResource.php
Responds to entity PATCH requests.
EntityResource::post in core/modules/rest/src/Plugin/rest/resource/EntityResource.php
Responds to entity POST requests and saves the new entity.

File

core/modules/rest/src/Plugin/rest/resource/EntityResource.php, line 212
Contains \Drupal\rest\Plugin\rest\resource\EntityResource.

Class

EntityResource
Represents entities as resources.

Namespace

Drupal\rest\Plugin\rest\resource

Code

protected function validate(EntityInterface $entity) {
  $violations = $entity
    ->validate();

  // Remove violations of inaccessible fields as they cannot stem from our
  // changes.
  $violations
    ->filterByFieldAccess();
  if (count($violations) > 0) {
    $message = "Unprocessable Entity: validation failed.\n";
    foreach ($violations as $violation) {
      $message .= $violation
        ->getPropertyPath() . ': ' . $violation
        ->getMessage() . "\n";
    }

    // Instead of returning a generic 400 response we use the more specific
    // 422 Unprocessable Entity code from RFC 4918. That way clients can
    // distinguish between general syntax errors in bad serializations (code
    // 400) and semantic errors in well-formed requests (code 422).
    throw new HttpException(422, $message);
  }
}