You are here

function number_field in Content Construction Kit (CCK) 5

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

Implementation of hook_field().

File

./number.module, line 138
Defines numeric field types.

Code

function number_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $allowed_values = number_allowed_values($field);
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_field = $field['field_name'] . '][' . $delta . '][value';
          if ($item['value'] != '') {
            if (is_numeric($field['min']) && $item['value'] < $field['min']) {
              form_set_error($error_field, t('The value of %name 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_field, t('The value of %name may be no larger than %max.', array(
                '%name' => t($field['widget']['label']),
                '%max' => $field['max'],
              )));
            }
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
              form_set_error($error_field, t('Illegal value for %name.', array(
                '%name' => t($field['widget']['label']),
              )));
            }
          }
        }
      }
      break;
  }
}