I am working on a simple form for searching a collection of marriage licenses. It consists of a select box where the user may choose to search last names, first names, or both, and a text field where they type the name they're searching for.

Everything is working fine so far, except that the user input is not retained after the form is submitted. For example, if I choose "Last names" and enter "Olsen", after the form is submitted the search box will be empty and the select box will revert to its default of "Both first and last names".

Here is my buildForm() method:

public function buildForm(array $form, FormStateInterface $form_state){
    // The select box.
    $form['target'] = [
        '#title' => $this->t('Search'),
        '#type' => 'select',
        '#options' => [
            'both' => $this->t('Both first and last names'),
            'last' => $this->t('Last names'),
            'first' => $this->t('First names'),
        '#default_value' => $form_state->getValue('target'),

    // The search box.
    $form['q'] = [
        '#title' => $this->t('for'),
        '#type' => 'textfield',
        '#default_value' => $form_state->getValue('q'),

    $form['actions'] = ['#type' => 'actions'];

    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Go'),

    return $form;

As you can see, I've tried using #default_value to set a default value by calling the form state to get whatever the user picked/entered. But alas, this does not seem to have done it.

How do I get it to display the user input in the form after submission?

  • 1
    what happens in your submitForm() ? Commented Feb 21, 2020 at 21:51
  • Currently, nothing; it's a stub containing a comment that says "Do the search and return the results". I hadn't gotten that far yet. Is this something that gets set in that stage? Commented Feb 21, 2020 at 22:08

1 Answer 1


Try adding $form_state->setRebuild(TRUE); to your submitForm(). Quoting from here:

Normally, after the entire form processing is completed and submit handlers have run, a form is considered to be done and \Drupal\Core\Form\FormSubmitterInterface::redirectForm() will redirect the user to a new page using a GET request (so a browser refresh does not re-submit the form). However, if 'rebuild' has been set to TRUE, then a new copy of the form is immediately built and sent to the browser, instead of a redirect.

I think that as you have stubbed a submit handler, but not implemented anything (and so are presumably not calling parent::submitForm either), it is not considered "done", so it is not redirecting; but it is not rebuilding based on the new $form_state either, because you haven't told it to. So you end up just seeing the original form.

As well as setting rebuild to true you may also need to $form_state->disableRedirect(TRUE) to get the desired behaviour of staying on the form, but with inputted values showing.

  • I added calls to disableRedirect(true) and setRebuild(true) on the $form_state in my submitForm(), and it now remembers the user's input correctly. Thank you! Commented Feb 21, 2020 at 22:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.