Problem/Motivation

$form_state storage disappears after form submit+F5

Steps to reproduce

1. Create simple form:

  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Click me (' . (int)$form_state->get('counter') . ')',
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $form_state->set('counter', (int)$form_state->get('counter') + 1);
    $form_state->setRebuild();
  }

2. Click several times on button.

3. Press F5 in browser.

CommentFileSizeAuthor
example_form.zip1.49 KBxandeadx
video2.gif419.54 KBxandeadx

Issue fork drupal-3419056

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

xandeadx created an issue. See original summary.

xandeadx’s picture

cilefen’s picture

What is the purpose of $form_state->setRebuild(); in the example code?

xandeadx’s picture

@cilefen disable form redirect

cilefen’s picture

Why is this a bug?

xandeadx’s picture

On F5 form submitted, but $form_state storage disappears

ankusht1515 made their first commit to this issue’s fork.

rushikesh raval’s picture

Status: Active » Needs review

It may not be bug. When you refresh or reload page it will build form again. So form state will reinitialize.

cilefen’s picture

Status: Needs review » Active

There is no code to review.

rushikesh raval’s picture

@cilefen I think it is not bug. It normal to reinitialize of variable on refresh. So no need for any code. But if @xandeadx needs to retain value then he may store value in session.

xandeadx’s picture

On F5 button form submitted, not just "refreshed". Browser send previous POST request. If just "refreshed" then button value would be = "Click me (0)" instead of "Click me (1)".

What's going on:
1. Drupal generate form with random form_build_id [1]
2. User submit form with form_build_id [1]
3. Submit callback modifies $form_state storage
4. Drupal generate new form_build_id [2] and save $form_state in key_value_expire table using form_build_id [2]
5. User submit form with form_build_id [2]
6. Drupal generate form_build_id [3], delete $form_state with form_build_id [2] from database, and save new $form_state with form_build_id [3]
7. User submit form again using F5 with form_build_id [2], but it doesn't exist anymore in key_value_expire table.

xandeadx’s picture

.

xandeadx’s picture

Title: $form_state storage disappears after form submit+F5 » $form_state storage disappears after form submit + submit on F5

Version: 10.2.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.