Memperluas jawaban (benar) dari Clive, saya bekerja melalui kode IFE. Saya tidak benar-benar membutuhkan seluruh modul yang didedikasikan untuk ini, jadi saya mengadopsi beberapa cuplikan di sana-sini untuk mendapatkan hasil yang saya butuhkan. Saya telah menandai jawabannya sebagai jawaban yang benar karena pada akhirnya itu adalah jawaban yang tepat.
Kode di bawah, semua kredit diberikan kepada Clive dan tim IFE - Saya hanya ingin menyajikan versi yang disederhanakan bagi siapa pun yang mencari jawaban yang sama.
// Standard gear - do some custom validation and set the errors
// as you go..
//
// Once all the validation has been done, call MODULE_errors_reset
// which will return an array of all errors against their ID.
// Expose this array however you like to your template, or loop
// over your form adding a #suffix to each element with an error
//
function MODULE_form_name_validate($form, &$form_state) {
drupal_set_message("validating...");
form_set_error("description", "There is an error here!!!!");
form_set_error("tags", "Yep, and here too!!!");
$reset_errors = MODULE_errors_reset( $form );
drupal_set_message( "<pre>" . print_r( $reset_errors, true ) . "</pre>" );
}
// This part is adopted from IFE. It's changed in two ways, it returns
// an array (which also merges with its recursive self).
// And it also skips all the 'display' stuff present in the original
// Essentially it extracts out the error messages from the session and unsets
// them. I am assuming that Drupal 7 marks the success of a validation based not
// whether the SESSION variable contains anything, the SESSION seems to be only
// for the message at the top of the screen.
//
function MODULE_errors_reset( $element ) {
if( ! isset( $_SESSION[ 'messages' ] ) ) {
return;
}
$reset_errors = array();
// Recurse through all children.
foreach( element_children( $element ) as $key ) {
if( isset( $element[ $key ] ) && $element[ $key ] ) {
$reset_errors += MODULE_errors_reset( $element[ $key ] );
}
}
// Check for errors and settings
$errors = form_get_errors();
$element_id = implode( '][', $element[ '#parents' ] );
if ( !empty( $errors[ $element_id ] )) {
$error_message = $errors[ $element_id ];
// Get error id
$error_id = array_search( $error_message, $_SESSION[ 'messages' ][ 'error' ] );
if( $error_id !== FALSE ) {
unset( $_SESSION[ 'messages' ][ 'error' ][ $error_id ] );
$_SESSION[ 'messages' ][ 'error' ] = array_values( $_SESSION[ 'messages' ][ 'error' ] );
if( count( $_SESSION[ 'messages' ][ 'error' ] ) <= 0 ) {
unset( $_SESSION[ 'messages' ][ 'error' ] );
}
$reset_errors[ $element[ '#id' ] ] = $error_message;
}
}
return $reset_errors;
}
// If there are no form errors, we still hit here, even after the 'reset', this is
// a good thing.
function MODULE_form_name_submit( $form, &$form_submit ) {
drupal_set_message("submited!");
}