You are here

function formatted_number_field_validate in Formatted Number 7

Implements hook_field_validate().

Possible error codes:

  • 'formatted_number_min': The value is less than the allowed minimum value.
  • 'formatted_number_max': The value is greater than the allowed maximum value.

File

./formatted_number.module, line 216
Defines numeric field types where the thousand and decimal separators are inherited from the Format Number API module.

Code

function formatted_number_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if ($item['value'] != '') {
      $min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : $field['settings']['min'];
      if (is_numeric($min) && $item['value'] < $min) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'formatted_number_min',
          'message' => t('%name: the value may be no less than %min.', array(
            '%name' => $instance['label'],
            '%min' => $min,
          )),
        );
      }
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : $field['settings']['max'];
      if (is_numeric($max) && $item['value'] > $max) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'formatted_number_max',
          'message' => t('%name: the value may be no greater than %max.', array(
            '%name' => $instance['label'],
            '%max' => $max,
          )),
        );
      }
    }
  }
}