function field_attach_validate

Perform 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

$entity_type: The type of $entity; e.g. 'node' or 'user'.

$entity: The entity with fields to validate.

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

Throws

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()
FieldAttachOtherTestCase::testFieldAttachValidate in drupal/modules/field/tests/field.test
Test field_attach_validate().
field_attach_form_validate in drupal/modules/field/field.attach.inc
Perform field validation against form-submitted field values.
ListDynamicValuesValidationTestCase::testDynamicAllowedValues in drupal/modules/field/modules/list/tests/list.test
Test that allowed values function gets the entity.
TaxonomyTermFieldTestCase::testTaxonomyTermFieldValidation in drupal/modules/taxonomy/taxonomy.test
Test term field validation.
TextFieldTestCase::testTextFieldValidation in drupal/modules/field/modules/text/text.test
Test text field validation.

File

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

Code

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

  // Validate $options since this is a new parameter added after Drupal 7 was
  // released.
  $options = is_array($options) ? $options : array();
  $errors = array();

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

  // Check field-type specific errors.
  _field_invoke('validate', $entity_type, $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_type, $entity, $errors);
  }
  if ($errors) {
    throw new FieldValidationException($errors);
  }
}