How do I retain user input in a form after submission? 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' =&gt; $this-&gt;t('Search'),
        '#type' =&gt; 'select',
        '#options' =&gt; [
            'both' =&gt; $this-&gt;t('Both first and last names'),
            'last' =&gt; $this-&gt;t('Last names'),
            'first' =&gt; $this-&gt;t('First names'),
        ],
        '#default_value' =&gt; $form_state-&gt;getValue('target'),
    ];


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

    $form['actions'] = ['#type' =&gt; 'actions'];

    $form['actions']['submit'] = [
        '#type' =&gt; 'submit',
        '#value' =&gt; $this-&gt;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?
Try adding $form_state-&gt;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-&gt;disableRedirect(TRUE) to get the desired behaviour of staying on the form, but with inputted values showing.