| 5 form.inc | form_get_errors() |
| 6 form.inc | form_get_errors() |
| 7 form.inc | form_get_errors() |
| 8 form.inc | form_get_errors() |
Returns an associative array of all errors.
Related topics
12 calls to form_get_errors()
File
- includes/
form.inc, line 1599 - Functions for form and batch generation and processing.
Code
function form_get_errors() {
$form = form_set_error();
if (!empty($form)) {
return $form;
}
}
Login or register to post comments
Comments
Using form_get_errors to skip cascading form validation
If you are stacking validation functions, even just applying a custom validation atop the FAPI default validation (like required fields), you can choose to *not* fire off your validation code until all previous validation has succeeded with a
if (form_get_errors()) return;as in:<?php/**
* Custom Form Validation
*/
function my_module_form_validate($form, &$form_state) {
// Don't custom-validate if previous validation errors (still) exist
if (form_get_errors()) return;
// .. Otherwise, process custom form validation goes here
}
?>
This would prevent some custom validation functionality to occur until all the "xyz form field required" have been resolved. And no, merely calling form_get_errors() does not pop the stack and prevent their display.
Filter out specific errors during validation
Not in a very clean way, but can be done:
(I'm planning to use something similar to this in the Conditional Fields module)
<?php
/**
* Custom Form Validation.
* Removes all form validation errors caused by a 'foo][bar' form element.
*/
function my_module_form_validate($form, &$form_state) {
$errors = form_get_errors();
if ($errors) {
// Clear errors.
form_clear_error();
// Clear error messages.
$error_messages = drupal_get_messages('error');
// Initialize an array where removed error messages are stored.
$removed_messages = array();
// Remove all errors originated by the 'foo][bar' element.
foreach ($errors as $name => $error_message) {
if ($name == 'foo][bar') {
$removed_messages[] = $error_message;
unset($errors[$name]);
}
}
// Reinstate remaining errors.
foreach ($errors as $name => $error) {
form_set_error($name, $error);
// form_set_error() calls drupal_set_message(), so we have to filter out
// these from the error messages as well.
$removed_messages[] = $error;
}
// Reinstate remaining error messages (which, at this point, are messages that
// were originated outside of the validation process).
foreach (array_diff($error_messages['error'], $removed_messages) as $message) {
drupal_set_message($message, 'error');
}
}
}
?>
Just what I was looking for.
Thank you for your contribution -- you saved me quite a bit of time.