Answers:
从Clive扩展了(正确的)答案,我研究了IFE代码。我实际上并不需要专用于此的整个模块,因此我在这里和那里采用了一些代码片段来获得所需的结果。我将他的答案标记为正确,因为这最终是正确的答案。
代码在下面,所有的功劳都归Clive和IFE团队所有-我只是想为寻求类似答案的任何人提供简化版本。
// 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!");
}