2

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

<?php

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) {
        \Drupal::service('page_cache_kill_switch')->trigger();

    $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) {
        $form_state->setRedirect('test.new');
    }

}

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 .

EDITED :

.routing.yml

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

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

3 Answers 3

3

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.

8
  • Good observation. I didn't even realize that.
    – Jaypan
    Mar 27, 2017 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
    – user60258
    Mar 27, 2017 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, 2017 at 14:52
  • 2
    Kevin is correct.
    – Jaypan
    Mar 27, 2017 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, 2017 at 14:56
1

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();
1
  • 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, 2017 at 13:20
0

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

\Drupal::service('page_cache_kill_switch')->trigger();

Remove that and see if your redirect works.

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

Your Answer

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