1.20.x text.module text_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors)

Implements hook_field_validate().

Possible error codes:

  • 'text_value_max_length': The value exceeds the maximum length.
  • 'text_summary_max_length': The summary exceeds the maximum length.

File

modules/field/modules/text/text.module, line 106
Defines simple text field types.

Code

function text_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    // @todo The maximum length can be exceeded because length is counted
    // separately for summary and value.
    foreach (array('value', 'summary') as $column) {
      if (!empty($item[$column])) {
        if (!empty($field['settings']['max_length']) && backdrop_strlen($item[$column]) > $field['settings']['max_length']) {
          switch ($column) {
            case 'value':
              $message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;

            case 'summary':
              $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;
          }
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => "text_{$column}_length",
            'message' => $message,
          );
        }
      }
    }
  }
}