function hook_field_validate

Validate this module's field data.

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

Parameters

\Drupal\Core\Entity\EntityInterface $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_reference_field_validate in drupal/core/modules/entity_reference/entity_reference.module
Implements hook_field_validate().
field_test_field_validate in drupal/core/modules/field/tests/modules/field_test/field_test.field.inc
Implements hook_field_validate().
number_field_validate in drupal/core/modules/number/number.module
Implements hook_field_validate().
options_field_validate in drupal/core/modules/options/options.module
Implements hook_field_validate().
taxonomy_field_validate in drupal/core/modules/taxonomy/taxonomy.module
Implements hook_field_validate().

... See full list

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

File

drupal/core/modules/field/field.api.php, line 400

Code

function hook_field_validate(\Drupal\Core\Entity\EntityInterface $entity = NULL, $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'],
          )),
        );
      }
    }
  }
}