field_attach_validate

7 field.attach.inc field_attach_validate($entity_type, $entity)
8 field.attach.inc field_attach_validate($entity_type, $entity)

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.

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()

File

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

Code

function field_attach_validate($entity_type, $entity) {
  $errors = array();
  // Check generic, field-type-agnostic errors first.
  _field_invoke_default('validate', $entity_type, $entity, $errors);
  // Check field-type specific errors.
  _field_invoke('validate', $entity_type, $entity, $errors);

  // 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

is it a bug?

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?

Login or register to post comments