Preserve modified values when going back

Created on 22 July 2024, 11 months ago

Problem/Motivation

When a multistep form is configured, when the user modifies one step and goes back, the modified values are lost once he/she comes back to the next step.

Steps to reproduce

  1. Create a content type with title and body.
  2. Create two filed groups of type From step, the second with the back button enabled.
  3. Place the title in the first step, and the body in the second step.
  4. Create a node of that type, set the title, and go to the next step.
  5. On this second step, modify the body's content, and click back.
  6. On the first step, click next to go back to the second step.
  7. The body is empty.
  8. The step field values are saved just if the next button gets clicked. Any change in the step is lost if the back button is clicked.

Question

Is there any configuration or way to keep the updated values when the back is clicked?

Thanks

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇪🇸Spain espurnes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @espurnes
  • 🇪🇸Spain espurnes

    After some digging I was not able to keep the $form[$child_id]['#access'] = FALSE; in the MultistepController rebuildForm method and preserve the updated step fields values when going back to the previous step.

    So, even if it is not the best option, I decided to add a new configuration (use_hidden_flow) at step level to change the $form[$child_id]['#access'] = FALSE; by a $form[$child_id]['#attributes']['class'][] = 'hidden';.

    If in a form with 3 steps, we enable the use_hidden_flow, all the fields will be rendered (but hidden with css) on each step.

    If in step 2, when going back to step 1, the step 2 changes are applied. So, when returning to step 2 the fields do have the previous values.

    If the new parameter use_hidden_flow is not enabled, the module continues working as before.

  • 🇪🇸Spain espurnes

    Patch with the previously described changes.

  • 🇵🇹Portugal dxvargas

    This seems to me like a bug and any solution shouldn't be optional.
    The described problem doesn't happen when clicking "Next"? Only when clicking "Back"? What is the difference? Why Next works and Back does not?

    The patch in #3 is fixes the problem you've described but I'm not sure it's the best option.
    I've tested in a entity reference field and in that case it doesn't fix the problem.

  • 🇵🇹Portugal dxvargas

    Ok, I've found a problem when making the "Back" button behave like "Next": the validation will work.
    That is a problem. For instance, if the user is going back, this will force the fields to be filled and prevent him from going back otherwise. Not good.

  • Pipeline finished with Success
    20 days ago
    Total: 234s
    #500016
  • Pipeline finished with Success
    20 days ago
    #500017
  • 🇵🇹Portugal dxvargas

    I've created a MR with the approach proposed in #3.

    But without the change being an option in the configuration.
    I don't think that deleting the fields already filled when going back should be an option, never.
    Also, I think it complicates the configuration page. The helper text in #3 says: "Hide the other steps fields instead of removing them from the current step.". Someone reading this for the first time wouldn't have a clue about the implications.
    There is a good explanation in the code:

    // Comment about the added use_hidden_flow:
    // The #access = FALSE is used to do not add the element
    // on other steps. As a sideeffect, the altered elements
    // do not persist if the back button is clicked.
    // To preserve the updated values even when clicking back
    // the #access = FALSE code is removed and replaced by a
    // hidden class. All the elements will be there, but will
    // be hidden to the user.
    // The use_hidden_flow is a new configuration set at step
    // level, so the new code will just affect those steps with
    // the use_hidden_flow set to true, by default is false.

    But putting all this explanation in the configuration form would make it too complex.

    If this goes just like that it may raise backward compatibility issues in some sites that for some reason test the "#access" property of the fields. So, I propose to merge this in a major release.

    If as proposed in #3 the usage of "#access" is configurable, then it won't have backward compatibility issues.

  • Pipeline finished with Success
    19 days ago
    Total: 162s
    #500612
  • Pipeline finished with Success
    19 days ago
    Total: 161s
    #500735
  • Pipeline finished with Failed
    19 days ago
    Total: 198s
    #500745
  • Pipeline finished with Failed
    19 days ago
    Total: 216s
    #500766
  • Pipeline finished with Success
    19 days ago
    Total: 159s
    #500776
  • Pipeline finished with Success
    19 days ago
    Total: 178s
    #500786
  • 🇪🇸Spain espurnes

    The solution I’ve submitted in #3 addresses the issue described above. While I acknowledge it might not be the most elegant or optimal fix, I needed to apply a quick solution in a live environment. For that reason, I added the patch with optional settings, ensuring backwards compatibility. This approach allows others in a similar situation to resolve the issue without affecting existing implementations.

    I'm glad you are suggesting improvements :). Let's see if any maintainer can check it.

    By the way, I’ve noticed there hasn’t been any maintenance activity on this module since March 2024.

  • 🇧🇪Belgium herved

    I think there are issues with the approach of hiding form elements with CSS.
    In our case a textarea element in the form has the hidden class but another class (form-element) applied on it overrides the display so the element is visible on all steps.
    Also, doing this means that it completely changes the validation as it requires all required fields in next steps to be filled in while the original implementation of this module does not.

    entity_form_steps has an interesting take on it, where they preserve values when going back but only keep the valid ones.
    See EntityFormSteps::validatePreviousForm. This would involve also changing #limit_validation_errors and simple_multistep_register_back.

Production build 0.71.5 2024