You are here

function payment_form_process_amount_validate in Payment 7

Implements form validate callback for a payment_amount element.

1 string reference to 'payment_form_process_amount_validate'
payment_element_info in ./payment.module
Implements hook_element_info().

File

./payment.ui.inc, line 682
The Payment user interface.

Code

function payment_form_process_amount_validate(array $element, array &$form_state) {
  $value = $element['#value'];

  // Do nothing if there is no value and the element is optional.
  if (!$element['#required'] && $value === '') {
    return;
  }

  // Quickly check for invalid characters.
  if (preg_match('#[^-\\d.,]#', $value)) {
    form_error($element, t('The amount can only consist of a minus sign, decimals and one decimal mark.'));
  }
  elseif (!preg_match('/^
    -?    # One optional minus sign.
    \\d+?  # One or more digits.
    [.,]? # One period or comma as optional decimal separator.
    \\d*   # Zero or more decimals.
    $/x', $value)) {
    form_error($element, t('The amount can only consist of digits, optionally preceded by a minus sign and optionally preceded, separated or succeeded by a decimal separator.'));
  }
  else {

    // Convert the value to a float.
    $amount = (double) $value;

    // Confirm the amount lies within the allowed range.
    if ($element['#minimum_amount'] !== FALSE && $amount < $element['#minimum_amount']) {
      form_error($element, t('The minimum amount is !amount.', array(
        '!amount' => payment_amount_human_readable($element['#minimum_amount'], $element['#currency_code']),
      )));
    }
    else {
      form_set_value($element, $amount, $form_state);
    }
  }
}