You are here

function fraction_field_validate in Fraction 7

Implements hook_field_validate().

Possible error codes:

  • 'fraction_min': The value is less than the allowed minimum value.
  • 'fraction_max': The value is greater than the allowed maximum value.

File

./fraction.field.inc, line 119
Fraction Field API functions

Code

function fraction_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    $value = '';
    if (!empty($item['decimal']) && is_numeric($item['decimal'])) {
      $value = $item['decimal'];
    }
    if (!empty($item['numerator']) && is_numeric($item['numerator']) && !empty($item['denominator']) && is_numeric($item['denominator'])) {
      $fraction = fraction($item['numerator'], $item['denominator']);
      $value = $fraction
        ->toDecimal(0, TRUE);
    }
    if ($value != '') {
      if (is_numeric($instance['settings']['min']) && $value < $instance['settings']['min']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'fraction_min',
          'message' => t('%name: the value may be no less than %min.', array(
            '%name' => $instance['label'],
            '%min' => $instance['settings']['min'],
          )),
        );
      }
      if (is_numeric($instance['settings']['max']) && $value > $instance['settings']['max']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'fraction_max',
          'message' => t('%name: the value may be no greater than %max.', array(
            '%name' => $instance['label'],
            '%max' => $instance['settings']['max'],
          )),
        );
      }
    }
  }
}