How can I change form action in drupal 8?

In default, page not change and when my form path is /site/drupal/page, my form action is <form data-drupal-selector="test" action="/site/drupal/page" method="post" id="test" accept-charset="UTF-8">.

But I want change it to other page. How can I do this ?

** Redirect not worked, see this link: Redirect after submit form


In the form submit handler, you can modify the $form['action'] value.

// Get the routename
$routename = 'My-Route';
  $url = \Drupal\Core\Url::fromRoute($routename);
  // make the redirection
  $form['#action'] = $url->toString();

in the submit form write a redirect

use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

//add the below in submit form handler. Use the route name you want to redirect
$response = new RedirectResponse(Url::fromRoute('<front>')->toString());


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.