function field_attach_validate

Performs field validation against the field data in an entity.

This function does not perform field widget validation on form submissions. It is intended to be called during API save operations. Use field_attach_form_validate() to validate form submissions.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity with fields to validate.

array $options: An associative array of additional options. See field_invoke_method() for details.

Throws

Drupal\field\FieldValidationException If validation errors are found, a FieldValidationException is thrown. The 'errors' property contains the array of errors, keyed by field name, language and delta.

Related topics

5 calls to field_attach_validate()
FieldAttachOtherTest::testFieldAttachValidate in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field_attach_validate().
field_attach_form_validate in drupal/core/modules/field/field.attach.inc
Performs field validation against form-submitted field values.
OptionsDynamicValuesValidationTest::testDynamicAllowedValues in drupal/core/modules/options/lib/Drupal/options/Tests/OptionsDynamicValuesValidationTest.php
Test that allowed values function gets the entity.
TermFieldTest::testTaxonomyTermFieldValidation in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php
Test term field validation.
TextFieldTest::testTextFieldValidation in drupal/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php
Test text field validation.

File

drupal/core/modules/field/field.attach.inc, line 1044
Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.

Code

function field_attach_validate(EntityInterface $entity, array $options = array()) {

  // Ensure we are working with a BC mode entity.
  $entity = $entity
    ->getBCEntity();
  $errors = array();

  // Check generic, field-type-agnostic errors first.
  $null = NULL;
  _field_invoke_default('validate', $entity, $errors, $null, $options);

  // Check field-type specific errors.
  _field_invoke('validate', $entity, $errors, $null, $options);

  // Let other modules validate the entity.
  // Avoid module_invoke_all() to let $errors be taken by reference.
  foreach (module_implements('field_attach_validate') as $module) {
    $function = $module . '_field_attach_validate';
    $function($entity, $errors);
  }
  if ($errors) {
    throw new FieldValidationException($errors);
  }
}