I am trying to redirect users to the anchor of the error form field after form validation.

In the form I set the form validation handler with the following code.

$form['#validate'] = 'my_form_validate';

In the validation handler, if the form shows some error message, I am trying to scroll to error field.

function my_form_validate() {
    $form_state['redirect'] = 'node/' . $form['nid']['#value'] . '#' . $error_field_anchor;

The form is not redirecting after validation.

I have tried other paths, such as user/1, node/123 without anchor but the form is not getting redirected to that one.

  • Try drupal_goto($path);
    – Bipin K
    Commented Aug 12, 2015 at 12:50
  • I try function drupal_goto(), and it's work and redirect, but form_validation message not displaying. This is one difference between form_state['redirect'] and drupal_goto() drupal.findincity.net/ppst/63547274810018958054256/… this article is answered why drupal_goto() not correct for this case. Commented Aug 12, 2015 at 13:02
  • $form['#validate'] = 'my_form_validate' replaces any existing validation handler, which may explain why the redirect isn't happening: No validation error is set. This is probably a task to do with JavaScript as what you want to achieve is scrolling the page. Unfortunately, we cannot say why the redirection doesn't take place, since we don't know the code that checks when users should be redirected (which could be wrong code) nor in which hook is the form validation handler added (which could target the wrong form or not form at all).
    – apaderno
    Commented Aug 22, 2021 at 7:22

2 Answers 2


Use this and put in you form

$form_state['redirect'] = 'url';
  • Nothing changed. After submit wrong form and validation, error message apear and form not redirected. Commented Aug 12, 2015 at 14:23

What you're looking for is drupal_redirect_form($form_state). In the validation function, set the redirect element in $form_state to the parameters used by drupal_goto().

function my_form_validate() {
  // @TODO: Add validation function check
  $form_state['redirect'] = array('node/' . $form['nid']['#value'], array('fragment' => $error_field_anchor));
  • 2
    calling drupal_redirect_form will abort any further form validation. If you are 100% positive that this validate function is the only one that needs to be called, then this is fine. However, if your form may be altered or needs further validation, this approach will break it. Commented Apr 20, 2017 at 16:44
  • also, FWIW, doesn't work on Drupal 7 Commented Apr 20, 2017 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.