Work with form validation errors

Investigating the function form_set_error I find functionality to work with it so far never used. On the one hand we have the faults so far in the form.  Suppose a form called test and want to know all the validation errors that will occur.

/**
 * Implementation of hook_form_alter().
 */
function test_form_alter(&$form, &$form_alter, $form_id) {
  if ($form_id == 'test_form') {
    $form['#validate'][] = 'test_form_validate_errors';
  }
}

function test_form_validate_errors($form, &$form_state) {
  $errors = form_set_error(NULL, NULL);
}

 

Once this is done in $errors have an array of all the validation errors that have the form. But suppose you want to reset the validation errors and process them and display them at will. Test_form_validate_errors then have the following:

function test_form_validate_errors($form, &$form_state) {
  $errors = form_set_error(NULL, NULL);

  //Reset errors
  form_set_error(NULL, NULL, TRUE);

  //Process errors
  .
  .
  .  
}

Comments

Fran's picture

I didn't know it!!! It should be usefull to customize your validations overwriting all validations hooks definied. But, what happens when two modules use form_set_error(null, null, true) ?

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.