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

File

modules/field/field.attach.inc, line 781

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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.