1

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

I have such form and:

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

Then, in validation function, if the form has 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;
    ...
}

But my form is not redirecting after validation.

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

  • Try drupal_goto($path); – Bipin K Aug 12 '15 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. – Vadim Sudarikov Aug 12 '15 at 13:02
0

Use this and put in you form

$form_state['redirect'] = 'url';
| improve this answer | |
  • Nothing changed. After submit wrong form and validation, error message apear and form not redirected. – Vadim Sudarikov Aug 12 '15 at 14:23
0

What you're looking for is drupal_redirect_form($form_state). In the validation function, set the redirect element in $form_state to 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));
  drupal_redirect_form($form_state);
}
| improve this answer | |
  • 1
    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. – aaronbauman Apr 20 '17 at 16:44
  • also, FWIW, doesn't work on Drupal 7 – aaronbauman Apr 20 '17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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