I created a form. When the form is submitted, I want redirect to the form page and pass an additional parameter.

I do this :

public function buildForm(array $form, FormStateInterface $form_state, $results = null)
  if ($results) {
    customlog($results, "RESULTS !!!"); // write in a log file
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Submit'),
  return $form;

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

public function submitForm(array &$form, FormStateInterface $form_state) {
  $results = array();
  // do some work ...
  // ...
  // $results is a simple associative array
  customlog("I pass here !", __FUNCTION__); // write in a log file
  return \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyCustomForm', $results);

My issue is when I submit this form, the page indefinitely load. When I check my log file, I realize that the "I pass here !" log is repeated indefinitely.

Where is my mistake ? When the form is submitted, how can I redirect to the same form with an additional parameter without this infinit loop ?

Thank you :)

  • 1
    That last line makes no sense. But I think for what you're trying to do you should store whatever persisten value you need in $form_state->set('somekey', 'somevalue') and add $form_state->setRebuild(TRUE) in the submit handler. In the form try to access the values as $form_state->get('somekey'). – user21641 Oct 5 '16 at 7:16

If you want to redirect to the form page you need to do something like this in your submit handler:

$form_state->setRedirect('NAME_OF_THE_ROUTE_OF_YOUR_FORM', $route_paramters, $options);

Maybe you can insert your results by query parameters?! Or save them in a session so that they can be used again. But the $form_state->setRebuild seems to be smarter to me.

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.