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.

| Comment | File | Size | Author |
|---|---|---|---|
| example_form.zip | 1.49 KB | xandeadx | |
| video2.gif | 419.54 KB | xandeadx |
Issue fork drupal-3419056
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
Comment #2
xandeadx commentedComment #3
cilefen commentedWhat is the purpose of
$form_state->setRebuild();in the example code?Comment #4
xandeadx commented@cilefen disable form redirect
Comment #5
cilefen commentedWhy is this a bug?
Comment #6
xandeadx commentedOn F5 form submitted, but $form_state storage disappears
Comment #8
rushikesh raval commentedIt may not be bug. When you refresh or reload page it will build form again. So form state will reinitialize.
Comment #9
cilefen commentedThere is no code to review.
Comment #10
rushikesh raval commented@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.
Comment #11
xandeadx commentedOn 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_statestorage4. Drupal generate new
form_build_id [2]and save$form_stateinkey_value_expiretable usingform_build_id [2]5. User submit form with
form_build_id [2]6. Drupal generate
form_build_id [3], delete$form_statewithform_build_id [2]from database, and save new$form_statewithform_build_id [3]7. User submit form again using F5 with
form_build_id [2], but it doesn't exist anymore inkey_value_expiretable.Comment #12
xandeadx commented.
Comment #13
xandeadx commented