You are here

function money_field_validate in Money field 7

Implements hook_field_validate().

Possible error codes:

  • 'money_min': The value is less than the allowed minimum value.
  • 'money_max': The value is greater than the allowed maximum value.
  • 'money_currency': Currency is missing.
  • 'money_amount': Amount is missing.

File

./money.module, line 164
This module defines the Money field.

Code

function money_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if ($item['amount'] != '') {
      $min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : $field['settings']['min'];
      if (is_numeric($min) && $item['amount'] < $min) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'money_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['amount'] > $max) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'money_max',
          'message' => t('%name: the value may be no greater than %max.', array(
            '%name' => $instance['label'],
            '%max' => $max,
          )),
        );
      }
      if (empty($item['currency'])) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'money_currency',
          'message' => t('%name: currency is required when an amount is specified.', array(
            '%name' => $instance['label'],
          )),
        );
      }
    }
    if (!is_numeric($item['amount']) && $item['currency']) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'money_amount',
        'message' => t('%name: a valid amount is required when a currency is specified.', array(
          '%name' => $instance['label'],
        )),
      );
    }
  }
}