You are here

function format_number_numericfield_process in Format Number API 6

Same name and namespace in other branches
  1. 7 format_number.module \format_number_numericfield_process()

Process an individual numeric form element.

Parameters

$element: The form element being processed.

$edit: The incoming POST data to populate the form element.

$form_state: A keyed array containing the current state of the form.

$form: An associative array containing the structure of the form.

1 string reference to 'format_number_numericfield_process'
format_number_elements in ./format_number.module
Implementation of hook_elements().

File

./format_number.module, line 407
This module provides a method to configure number formats (site default and user defined) with configurable decimal point and thousand separators. It also exposes several functions that can be used by other contributed or custom modules to display…

Code

function format_number_numericfield_process($element, $edit, $form_state, $form) {
  $element_precision = isset($element['#precision']) && (int) $element['#precision'] > 0 ? (int) $element['#precision'] : 12;
  $element_decimals = isset($element['#decimals']) && (int) $element['#decimals'] >= 0 ? (int) $element['#decimals'] : 0;
  $element_minimum = isset($element['#minimum']) ? parse_formatted_number($element['#minimum']) : NULL;
  if (!is_numeric($element_minimum)) {
    $element_minimum = format_number_compute_boundary('lower', $element_precision, $element_decimals);
  }

  // Compute size and maxlength for the input element, but still allow the
  // user specify these values in the form definition.
  if (empty($element['#size']) || empty($element['#maxlength'])) {
    $element_maxlength = $element_precision;
    if ($element_decimals > 0) {
      $element_maxlength++;
    }
    if (isset($element_minimum) && $element_minimum < 0) {
      $element_maxlength++;
    }
    $thousands_sep = format_number_get_options('thousands_sep');
    if (!empty($thousands_sep)) {
      $element_maxlength += ceil(($element_precision - $element_decimals) / 3) - 1;
    }
    if (empty($element['#size'])) {
      $element['#size'] = $element_maxlength + 1;
    }
    if (empty($element['#maxlength'])) {
      $element['#maxlength'] = $element_maxlength;
    }
  }

  // Format the element value. A valid PHP number is expected. It may come from
  // user defined form or from element value callback. In any case, our element
  // validation callback will generate a valid PHP number, or flag an error. Our
  // goal now is generate a number formatted with site or user defined options,
  // if value is a valid PHP number.
  $value = !empty($element['#value']) && is_string($element['#value']) ? $element['#value'] : '';
  if (is_numeric($value)) {
    $value = format_number($value, $element_decimals);
  }
  $element['#value'] = $value;

  // Pass decimal places to the client-side javascript using HTML attribute.
  if (isset($element['#attributes']) && is_array($element['#attributes'])) {
    $element['#attributes']['decimals'] = $element_decimals;
  }
  else {
    $element['#attributes'] = array(
      'decimals' => $element_decimals,
    );
  }

  // Attach a validation callback to the form element.
  if (isset($element['#element_validate']) && is_array($element['#element_validate'])) {
    array_shift($element['#element_validate'], 'format_number_numericfield_validate');
  }
  else {
    $element['#element_validate'] = array(
      'format_number_numericfield_validate',
    );
  }
  return $element;
}