function money_validate_field_value in Money field 6
Helper function to validate a money field.
Validates the currency and its relation with the amount. Both fields must be specified, or none.
Parameters
array $field: The field array.
number $amount: The number that should be validated.
number $currency: The currency that should be validated.
Return value
array An array with error messages or empty if value is correct.
1 call to money_validate_field_value()
- money_field in ./
money.module - Implementation of hook_field().
File
- ./
money.module, line 295 - This module defines the Money CCK field.
Code
function money_validate_field_value($field, $amount, $currency) {
$widget_label = t($field['widget']['label']);
$errors = array();
if (empty($currency)) {
if ($field['required']) {
$errors[] = t('%name: Currency is required.', array(
'%name' => $widget_label,
));
}
else {
if (is_numeric($amount)) {
$errors[] = t('%name: Currency is required when an amount is specified.', array(
'%name' => $widget_label,
));
}
}
}
else {
// When validating the default value in field settings panel, CCK is giving
// us the options at field level, not within the widget item of the field.
if (!empty($field['allowed_currencies'])) {
$allowed_currencies = isset($field['allowed_currencies']) ? array_filter($field['allowed_currencies']) : array();
$amount_required = FALSE;
}
else {
$allowed_currencies = isset($field['widget']['allowed_currencies']) ? array_filter($field['widget']['allowed_currencies']) : array();
$amount_required = TRUE;
}
// When no currency is enabled, allow them all.
if (empty($allowed_currencies)) {
$allowed_currencies = currency_api_get_list();
}
if (!isset($allowed_currencies[$currency])) {
if (!$field['required']) {
$errors[] = t('%name: The currency %currency is not allowed.', array(
'%name' => $widget_label,
'%currency' => $currency,
));
}
}
else {
if (!is_numeric($amount) && $amount_required) {
$errors[] = t('%name: A valid amount is required when a currency is specified.', array(
'%name' => $widget_label,
));
}
}
}
return $errors;
}