7.x field.attach.inc field_attach_validate($entity_type, $entity, $options = array())

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

Comments

georgir’s picture

If you programmatically set non-numeric data in an integer field and then try to validate the entity with this function, it does not throw any errors.
Instead, the following entity save will throw an exception if your MySQL server is in strict mode, or just treat the invalid value as zero if not.
That is quite bad, is it a bug or am I just not using it correctly?

joachim’s picture

Sounds like that's a bug.

mradcliffe’s picture

Surprisingly field_attach_validate doesn't validate whether a field instance is required or not. That is only set via field_attach_form and normal form validation.