function _webform_validate_number in Webform 7.4
Same name and namespace in other branches
- 6.3 components/number.inc \_webform_validate_number()
- 7.3 components/number.inc \_webform_validate_number()
A Drupal Form API Validation function.
Validates the entered values from number components on the client-side form.
Parameters
array $element: The form element. May either be a select or a webform_number element.
array $form_state: The full form state for the webform.
1 string reference to '_webform_validate_number'
- _webform_render_number in components/
number.inc - Implements _webform_render_component().
File
- components/
number.inc, line 621 - Webform module number component.
Code
function _webform_validate_number($element, &$form_state) {
// Trim spaces for basic cleanup.
$value = trim($element['#value']);
form_set_value($element, $value, $form_state);
if ($value != '') {
// First check that the entered value matches the expected value.
if (!webform_number_format_match($value, $element['#point'], $element['#separator'])) {
form_error($element, t('!name field value must format numbers as "@example".', array(
'!name' => $element['#title'],
'@example' => webform_number_format(12345.6789, $element['#decimals'], $element['#point'], $element['#separator']),
)));
return;
}
// Numeric test.
$numeric_value = webform_number_standardize($value, $element['#point']);
if (is_numeric($numeric_value)) {
// Range test.
if ($element['#min'] != '' && $element['#max'] != '') {
// Flip minimum and maximum if needed.
if ($element['#max'] > $element['#min']) {
$min = $element['#min'];
$max = $element['#max'];
}
else {
$min = $element['#max'];
$max = $element['#min'];
}
if ($numeric_value > $max || $numeric_value < $min) {
form_error($element, t('!name field value of @value should be in the range @min to @max.', array(
'!name' => $element['#title'],
'@value' => $value,
'@min' => $element['#min'],
'@max' => $element['#max'],
)));
}
}
elseif ($element['#max'] != '' && $numeric_value > $element['#max']) {
form_error($element, t('!name field value must be less than or equal to @max.', array(
'!name' => $element['#title'],
'@max' => $element['#max'],
)));
}
elseif ($element['#min'] != '' && $numeric_value < $element['#min']) {
form_error($element, t('!name field value must be greater than or equal to @min.', array(
'!name' => $element['#title'],
'@min' => $element['#min'],
)));
}
// Integer test.
if ($element['#integer'] && filter_var((double) $numeric_value, FILTER_VALIDATE_INT) === FALSE) {
form_error($element, t('!name field value of @value must be an integer.', array(
'!name' => $element['#title'],
'@value' => $value,
)));
}
// Step test.
$starting_number = $element['#min'] ? $element['#min'] : 0;
if ($element['#step'] != 0 && webform_modulo($numeric_value - $starting_number, $element['#step']) != 0.0) {
$samples = array(
$starting_number,
$starting_number + $element['#step'] * 1,
$starting_number + $element['#step'] * 2,
$starting_number + $element['#step'] * 3,
);
if ($starting_number) {
form_error($element, t('!name field value must be @start plus a multiple of @step. i.e. @samples, etc.', array(
'!name' => $element['#title'],
'@start' => $element['#min'],
'@step' => $element['#step'],
'@samples' => implode(', ', $samples),
)));
}
else {
form_error($element, t('!name field value must be a multiple of @step. i.e. @samples, etc.', array(
'!name' => $element['#title'],
'@step' => $element['#step'],
'@samples' => implode(', ', $samples),
)));
}
}
}
else {
form_error($element, t('!name field value of @value must be numeric.', array(
'!name' => $element['#title'],
'@value' => $value,
)));
}
}
}