Last active
February 2, 2016 07:44
-
-
Save rang501/68ff8e6ada893079fe7b to your computer and use it in GitHub Desktop.
Fix form errors order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change error messages order. | |
* | |
* We need to change the error messages order, because it's wrong. To do that, | |
* the session array must be modified. It's not nice, but gets the job done. | |
*/ | |
function my_module_validate_messages_order($form, $form_state) { | |
$errors = form_get_errors(); | |
// Skip, if there isn't any form errors. | |
if (empty($errors)) { | |
return; | |
} | |
// Clear errors. | |
form_clear_error(); | |
// Sort form elements to get the right order. | |
uasort($form, 'element_sort'); | |
foreach ($form as $key => $message) { | |
foreach (array_keys($errors) as $name) { | |
// If the element have a message. | |
if (strpos($name, $key) === 0) { | |
// Find the correct key form messages. | |
// It is not nice to use a session variable. | |
$k = array_search($errors[$name], $_SESSION['messages']['error']); | |
if ($k !== FALSE) { | |
// Unset the message. | |
unset($_SESSION['messages']['error'][$k]); | |
// Set error message again. | |
form_set_error($name, $errors[$name]); | |
} | |
} | |
} | |
} | |
} | |
function my_module_form_alter(&$form, $form_state) { | |
$form['#validate'][] = 'my_module_validate_messages_order'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment