function _webform_validate_number in Webform 6.3
Same name and namespace in other branches
- 7.4 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
$element: The form element. May either be a select or a webform_number element.
$form_state: The full form state for the webform.
Return value
None. Calls a form_set_error if the number is not valid.
1 string reference to '_webform_validate_number'
- _webform_render_number in components/
number.inc - Implements _webform_render_component().
File
- components/
number.inc, line 577 - 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 @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 @min.', array(
'%name' => $element['#title'],
'@min' => $element['#min'],
)));
}
// Integer test.
if ($element['#integer'] && !is_int($numeric_value * 1)) {
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) {
$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,
)));
}
}
}