function currency_form_currency_amount_validate in Currency 7.2
Implements form validate callback for a currency_amount element.
1 string reference to 'currency_form_currency_amount_validate'
- currency_element_info in currency/
currency.module - Implements hook_element_info().
File
- currency/
currency.module, line 291 - Provides currency information and allows users to add custom currencies.
Code
function currency_form_currency_amount_validate(array $element, array &$form_state) {
$value = $element['#value'];
$amount = $value['amount'];
$currency_code = isset($value['currency_code']) ? $value['currency_code'] : $element['#currency_code'];
try {
$amount = Input::parseAmount($amount);
} catch (AmountInvalidDecimalSeparatorException $e) {
form_error($element['amount'], t('The amount can only have no or one decimal separator and it must be one of %decimal_separators.', array(
'%decimal_separators' => implode(Input::$decimalSeparators),
)));
} catch (AmountNotNumericException $e) {
form_error($element['amount'], t('The amount can only consist of a minus sign, decimals and one decimal mark.'));
}
// Confirm the amount lies within the allowed range.
$currency = currency_load($currency_code);
if ($element['#minimum_amount'] !== FALSE && bccomp($element['#minimum_amount'], $amount, CURRENCY_BCMATH_SCALE) == 1) {
form_error($element['amount'], t('The minimum amount is !amount.', array(
'!amount' => $currency
->format($element['#minimum_amount']),
)));
}
elseif ($element['#maximum_amount'] !== FALSE && bccomp($amount, $element['#maximum_amount'], CURRENCY_BCMATH_SCALE) == 1) {
form_error($element['amount'], t('The maximum amount is !amount.', array(
'!amount' => $currency
->format($element['#maximum_amount']),
)));
}
// The amount in $form_state is a human-readable, optionally localized
// string, which cannot be used by other code. $amount is a numeric string
// after running it through Input::parseAmount().
form_set_value($element, array(
'amount' => $amount,
'currency_code' => $currency_code,
), $form_state);
}