7.x field.api.php hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors)

Validate this module's field data.

If there are validation problems, add to the $errors array (passed by reference). There is no return value.


$entity_type: The type of $entity.

$entity: The entity for the operation.

$field: The field structure for the operation.

$instance: The instance structure for $field on $entity's bundle.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.

$errors: The array of errors (keyed by field name, language code, and delta) that have already been reported for the entity. The function should add its errors to this array. Each error is an associative array with the following keys and values:

  • error: An error code (should be a string prefixed with the module name).
  • message: The human readable message to be displayed.

Related topics

6 functions implement hook_field_validate()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_form_field_validate in includes/common.inc
Attaches field API validation to entity forms.
field_test_field_validate in modules/field/tests/field_test.field.inc
Implements hook_field_validate().
list_field_validate in modules/field/modules/list/list.module
Implements hook_field_validate().
number_field_validate in modules/field/modules/number/number.module
Implements hook_field_validate().
taxonomy_field_validate in modules/taxonomy/taxonomy.module
Implements hook_field_validate().

... See full list

1 invocation of hook_field_validate()
field_attach_validate in modules/field/field.attach.inc
Perform field validation against the field data in an entity.


modules/field/field.api.php, line 404
Hooks provided by the Field module.


function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if (!empty($item['value'])) {
      if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'text_max_length',
          'message' => t('%name: the value may not be longer than %max characters.', array(
            '%name' => $instance['label'],
            '%max' => $field['settings']['max_length'],


EvanDonovan’s picture

See http://drupal.org/node/1226110 - you need to implement hook_field_widget_error() to show the messages that you define here for each error to the user.

The field_example.module in the Examples project has more information.

SlyK’s picture

You need manually add entity_form_field_validate() call in your entity form validation function. Otherwise hook_field_validate() will never be invoked automatically for your custom entity type (means no validation on 'list','number' and other fields).

On custom entity creation turtorials I used to learn drupal Entities was no words about entity_form_field_validate() function. And as for me it seems logical to make it automatically called somewhere in Entity module.

dutchyoda’s picture

I'm trying to create a max-uses validate.
So I'm trying to get all uses with the exception of the current $entity.
But if I want to extract the id of the $entity (entity_extract_ids or entity_id) I get NULL.
Is there a reason why the id is not known?

bineetchaubey’s picture

always getting empty value in $items in last parameter. What is mean by " empty array if unset " in following description $items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.