You are here

function range_field_validate in Range 7

Implements hook_field_validate().

Possible error codes:

  • 'range_both_required': Both FROM and TO values must be specified.
  • 'range_min': The FROM value is less than the allowed minimum value.
  • 'range_max': The TO value is greater than the allowed maximum value.
  • 'range_from_greater_than_to': FROM value is greater than TO value.

File

./range.module, line 193
Defines range field types.

Code

function range_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $instance = range_translate_field_instance($instance);
  foreach ($items as $delta => $item) {
    if ($item['from'] !== '' && $item['to'] === '' || $item['from'] === '' && $item['to'] !== '') {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'range_both_required',
        'message' => t('%name: you must specify two values.', array(
          '%name' => $instance['label'],
        )),
      );
    }
    if ($item['from'] != '' && $item['to'] != '') {
      if (is_numeric($instance['settings']['min']) && $item['from'] < $instance['settings']['min']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'range_min',
          'message' => t('%name: the FROM value may be no less than %min.', array(
            '%name' => $instance['label'],
            '%min' => $instance['settings']['min'],
          )),
        );
      }
      if ($item['from'] > $item['to']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'range_from_greater_than_to',
          'message' => t('%name: FROM value is greater than TO value.', array(
            '%name' => $instance['label'],
          )),
        );
      }
      if (is_numeric($instance['settings']['max']) && $item['to'] > $instance['settings']['max']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'range_max',
          'message' => t('%name: the TO value may be no greater than %max.', array(
            '%name' => $instance['label'],
            '%max' => $instance['settings']['max'],
          )),
        );
      }
    }
  }
}