Form redirection on submit | Drupal 8

Form redirection on submit

Submitted by editor on Fri, 01/15/2016 - 17:55
Question

How to redirect a form after submission

Example : (Insert this line in to your form submitForm() methode)

Redirect to the user page ( Redirection on form submit)
$form_state->setRedirect('user.page');

Redirect to a custom page
$form_state->setRedirect('your.module.route.name');

 

Like:

  public function submitForm(array &$form, FormStateInterface $form_state) {
   $form_state->setRedirect('your.module.route.name');
  }

Using a Url Object (use Drupal\Core\Url)

$url = Url::fromRoute('route.name', [
  'arg1' => "value1",
  'arg1' => "value2",
]);
$form_state->setRedirectUrl($url);

Comments

zaibatsu (not verified)

Thu, 03/17/2016 - 09:05

Greetings,

If i want to redirect a form to a view page? how can i do that?

$form_state->setRedirect('views.route.name'); ? how would i get the route name?

can you please help? Thanks

Pavel (not verified)

Fri, 04/28/2017 - 07:47

When i use $form_state->setRedirect('<front>'); i get nothing in $_POST array on front page. How can i send post data of form with setRedirect?

Add new comment