You are here

function number_decimal_validate in Content Construction Kit (CCK) 6.2

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

FAPI validation of an individual decimal element.

1 string reference to 'number_decimal_validate'
number_process in modules/number/number.module
Process an individual element.

File

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

Code

function number_decimal_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $field = content_fields($field_name, $type_name);
  $field_key = $element['#columns'][0];
  $value = $element['#value'][$field_key];
  if ($element[$field_key]['#required'] || !empty($value)) {
    $decimal = $element['#decimal'] ? $element['#decimal'] : '.';
    $start = $value;
    $value = preg_replace('@[^-0-9\\' . $decimal . ']@', '', $value);
    if ($start != $value) {
      $error_field = implode('][', $element['#parents']) . '][' . $field_key;
      form_set_error($error_field, t('Only numbers and the decimal character (%decimal) are allowed in %field.', array(
        '%decimal' => $element['#decimal'],
        '%field' => t($field['widget']['label']),
      )));
    }
    else {
      $value = str_replace($element['#decimal'], '.', $value);
      $value = round($value, $element['#scale']);
      form_set_value($element[$field_key], $value, $form_state);
    }
  }
}