I use this code in my form ( buildForm(array $form, FormStateInterface $form_state) ) :


namespace Drupal\test\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\user\PrivateTempStoreFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManager;
use Symfony\Component\HttpFoundation\Response;

class TestForm extends FormBase {

    public function getFormId() {
        return 'TESTOLDTONEW';

    public function buildForm(array $form, FormStateInterface $form_state) {

    $a['ad_mail'] = [
        '#type' => 'email',
        '#title' => t('Email ID:'),
        '#required' => TRUE,
    $b['total'] = [
        '#type' => 'hidden',
        '#attributes' => [
            'id' => 'actionsubmitid',
        '#value' => '0',
    $c['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Finalize the purchase'),
        '#button_type' => 'primary',
    $test= [
        '#theme' => 'test',
        '#a' => $a,
        '#b' => $b,
        '#c' => $c,

    return $test;

    public function validateForm(array &$form, FormStateInterface $form_state) {


    public function submitForm(array &$form, FormStateInterface $form_state) {


Now I want redirect to a machin name ( for example "test.new" ) that create in routing.yml file . I use $form_state->setRedirect('test.new'); in "submitForm(array &$form, FormStateInterface $form_state)" but not work .



  path: '/test/old'
    _form: '\Drupal\bpay\Form\TestoldForm'
    _permission: 'access content'

  path: '/test/new'
    _controller: '\Drupal\bpay\Controller\TestnewController::new'
    _permission: 'access content'
  • There is nothing in what you've described that would stop that redirect from happening. So your problem lies somewhere else.
    – Jaypan
    Mar 26 '17 at 6:51
  • No all of codes are this .
    – RobinSon
    Mar 26 '17 at 6:53
  • Ok. Your problem lies in the code you didn't show.
    – Jaypan
    Mar 26 '17 at 6:54
  • Code is work just redirect not work . How can I fix this ?
    – RobinSon
    Mar 26 '17 at 6:56
  • I don't know - I can't see your code
    – Jaypan
    Mar 26 '17 at 7:08

The buildForm method needs to return $form. Not $test. You're missing key items by ignoring the $form variable. Properly set the form items (not as their own variable) and remove the custom theme hook you're trying to do. Cut this down to its simplest pieces first just to get it working and go from there.

Get rid of $a $b $c variables as that doesn't make any sense, just use $form. Your submit info is not making it into the form itself, which is why it isn't working.

  • Good observation. I didn't even realize that.
    – Jaypan
    Mar 27 '17 at 11:56
  • I cant do this because I want use $c as theme and if I use all of them with $form , I cant use my theme and show all $form var in page without theme
    – RobinSon
    Mar 27 '17 at 14:51
  • 1
    That's wrong. You cannot just send elements from a form piecemeal like that. You have to handle it differently. You must allow the system to properly build and render these elements for it to work. Theming it is a different issue altogether.
    – Kevin
    Mar 27 '17 at 14:52
  • 2
    Kevin is correct.
    – Jaypan
    Mar 27 '17 at 14:54
  • 1
    I don't understand what test is supposed to be as a theme hook or what it's definition is. But just make the suggested changes and your form will act the way you expect. Then, open a separate issue about how to theme a form the way you want to.
    – Kevin
    Mar 27 '17 at 14:56

You can set the redirection in form action,

// Get the routename
$routename = 'test.new';
  $url = \Drupal\Core\Url::fromRoute($routename);
  // make the redirection
  $form['#action'] = $url->toString();
  • I don't think manually setting the #action is a best practice of Drupal. It should be handled in the redirect of the form.
    – Kevin
    Mar 27 '17 at 13:20

I think it may be because you are killing the cache with this:


Remove that and see if your redirect works.

  • When I change $a and $b and $c to $form and return $form, redirect worked but whit this dont work.
    – RobinSon
    Mar 26 '17 at 22:01
  • I remove that but still not worked.
    – RobinSon
    Mar 27 '17 at 4:15

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.