You are here

function editor_note_field_validate in Editor Notes 7

Implements hook_field_validate().

Defines messages that will be shown on validation of widget form using hook_field_widget_error().

File

./editor_note.module, line 737
Main functionality for Editor Notes module.

Code

function editor_note_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  if ($field['type'] == 'editor_note') {
    foreach ($items as $delta => $item) {
      if ($entity_type && $entity) {

        // Field widget form validation, otherwise field settings form.
        if (empty($item['note']) && $instance['required']) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'note',
            'message' => t('Field %name is required.', array(
              '%name' => $instance['label'],
            )),
          );
        }
      }
      if (isset($instance['settings']['text_processing']) && $instance['settings']['text_processing'] != EDITOR_NOTE_DEFAULT_TEXT_FORMAT) {
        $text_processing = TRUE;
      }

      // Maxlength check applies for plain text only.
      if (!$text_processing) {
        if (!empty($item['note']) && $field['settings']['notes_maxlength'] > 0) {
          if (drupal_strlen($item['note']) > $field['settings']['notes_maxlength']) {
            $errors[$field['field_name']][$langcode][$delta][] = array(
              'error' => 'notes_maxlength',
              'message' => t('%name: the value cannot not be longer than %max characters.', array(
                '%name' => $instance['label'],
                '%max' => $field['settings']['notes_maxlength'],
              )),
            );
          }
        }
      }

      // Validate first element since there can be only 1 textarea and 1 item
      // for adding notes.
      break;
    }
  }
}