Skip to content
Snippets Groups Projects
Commit eb412f7c authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #39179 by chx: improved validation of forms.

parent 0212f66f
No related branches found
No related tags found
No related merge requests found
...@@ -145,6 +145,24 @@ function _form_validate($elements, $form_id = NULL) { ...@@ -145,6 +145,24 @@ function _form_validate($elements, $form_id = NULL) {
if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') { if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') {
form_error($elements, t('%name field is required', array('%name' => $elements['#title']))); form_error($elements, t('%name field is required', array('%name' => $elements['#title'])));
} }
// Add legal choice check if element has #options.
if (isset($elements['#options']) && isset($elements['#value'])) {
$message = t('Illegal choice in %title.', array('%title' => theme('placeholder', $elements['#title'])));
if (is_array($elements['#value'])) {
$value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value'];
foreach ($value as $v) {
if (!isset($elements['#options'][$v])) {
form_error($elements, $message);
}
}
}
elseif (!isset($elements['#options'][$elements['#value']])) {
form_error($elements, $message);
}
}
// User-applied checks.
if (isset($elements['#validate'])) { if (isset($elements['#validate'])) {
foreach ($elements['#validate'] as $function => $args) { foreach ($elements['#validate'] as $function => $args) {
$args = array_merge(array($elements), $args); $args = array_merge(array($elements), $args);
......
...@@ -775,7 +775,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr ...@@ -775,7 +775,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr
'#return_value' => $format->format, '#return_value' => $format->format,
'#parents' => $parents, '#parents' => $parents,
'#description' => theme('filter_tips', _filter_tips($format->format, false)), '#description' => theme('filter_tips', _filter_tips($format->format, false)),
'#validate' => array('filter_form_validate' => array())
); );
} }
} }
...@@ -793,18 +792,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr ...@@ -793,18 +792,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr
return $form; return $form;
} }
function filter_form_validate($element) {
static $validated;
if ($validated) {
return;
}
$validated = 1;
$formats = filter_formats();
if (!isset($formats[$element['#value']])) {
form_set_error($element['#parents'][0], t('The supplied input format is invalid.'));
}
}
/** /**
* Returns true if the user is allowed to access this format. * Returns true if the user is allowed to access this format.
*/ */
......
...@@ -775,7 +775,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr ...@@ -775,7 +775,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr
'#return_value' => $format->format, '#return_value' => $format->format,
'#parents' => $parents, '#parents' => $parents,
'#description' => theme('filter_tips', _filter_tips($format->format, false)), '#description' => theme('filter_tips', _filter_tips($format->format, false)),
'#validate' => array('filter_form_validate' => array())
); );
} }
} }
...@@ -793,18 +792,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr ...@@ -793,18 +792,6 @@ function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = arr
return $form; return $form;
} }
function filter_form_validate($element) {
static $validated;
if ($validated) {
return;
}
$validated = 1;
$formats = filter_formats();
if (!isset($formats[$element['#value']])) {
form_set_error($element['#parents'][0], t('The supplied input format is invalid.'));
}
}
/** /**
* Returns true if the user is allowed to access this format. * Returns true if the user is allowed to access this format.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment