You are here

function number_field in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 5 number.module \number_field()
  2. 6.3 modules/number/number.module \number_field()
  3. 6 modules/number/number.module \number_field()

Implementation of hook_field().

File

modules/number/number.module, line 217
Defines numeric field types.

Code

function number_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $allowed_values = content_allowed_values($field);
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
          if (is_array($item) && isset($item['_error_element'])) {
            unset($item['_error_element']);
          }
          if ($item['value'] != '') {
            if (is_numeric($field['min']) && $item['value'] < $field['min']) {
              form_set_error($error_element, t('%name: the value may be no smaller than %min.', array(
                '%name' => t($field['widget']['label']),
                '%min' => $field['min'],
              )));
            }
            if (is_numeric($field['max']) && $item['value'] > $field['max']) {
              form_set_error($error_element, t('%name: the value may be no larger than %max.', array(
                '%name' => t($field['widget']['label']),
                '%max' => $field['max'],
              )));
            }
            if (count($allowed_values)) {

              // We cannot use array_key_exists() because allowed values are
              // stored as strings, and we need to compare numeric equality.
              $valid = FALSE;
              foreach ($allowed_values as $kay => $value) {
                if ((double) $item['value'] == (double) $kay) {
                  $valid = TRUE;
                  break;
                }
              }
              if (!$valid) {
                form_set_error($error_element, t('%name: illegal value.', array(
                  '%name' => t($field['widget']['label']),
                )));
              }
            }
          }
        }
      }
      return $items;
  }
}