function money_field in Money field 5
Same name and namespace in other branches
- 6 money.module \money_field()
Implementation of hook_field().
File
- ./
money.module, line 159 - This module defines the "money" CCK field. It uses the Currency API, which is included in the Currency module, to get a list of valid currencies.
Code
function money_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
$allowed_currencies = _money_parse_currencies($field['allowed_currencies']);
if (is_array($items)) {
foreach ($items as $delta => $item) {
// Generate the regular expression to validate the entered amounts.
$decimal_separator = preg_quote(_money_get_decimal_separator($field['decimal_separator']));
$digit_group_separator = preg_quote(_money_get_digit_group_separator($field['digit_group_separator']));
$regexp = "/^-?(((\\d{1,3}" . $digit_group_separator . ")?(\\d{3}" . $digit_group_separator . ")*(\\d{3}){1})|\\d+)(" . $decimal_separator . "\\d{1,2})?\$/";
// Validate the currency.
if (!in_array($item['currency'], $allowed_currencies)) {
form_set_error($field['field_name'] . '][' . $delta . '][currency', t('The currency %currency is not allowed.', array(
'%currency' => t($item['currency']),
)));
}
// Validate the amount:
// - allow empty value when the field is not required
// - ensure the amount was entered according to the format
if ($field['required'] && $item['amount'] == '') {
form_set_error($field['field_name'] . '][' . $delta . '][amount', t('You did not enter an amount.'));
}
else {
if (!empty($item['amount']) && !preg_match($regexp, $item['amount'])) {
form_set_error($field['field_name'] . '][' . $delta . '][amount', t('The amount is formatted invalidly.'));
}
}
}
}
break;
}
}