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 118
Defines simple text field types.

Code

function text_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {

    // @todo Length is counted separately for summary and value, so the maximum
    //   length can be exceeded very easily.
    foreach (array(
      'value',
      'summary',
    ) as $column) {
      if (!empty($item[$column])) {
        if (!empty($field['settings']['max_length']) && drupal_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,
          );
        }
      }
    }
  }
}