Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] [ComponentWithFormTrait] Force recreation of FormView on PreReRender hook to synchronize FormInstance and FormView. #2602

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from

Conversation

Mauriceu
Copy link

Q A
Bug fix? yes
New feature? no
Issues Fix #1981
License MIT

ComponentWithFormTrait will now recreate the existing FormView in its PreReRender-Hook submitFormOnRender() to synchronize FormView and FormInstance to reflect any manual changes to the FormInstance.
This primarily solves the Problem of manually added FormErrors not being rendered.

…forcing a recreation of the FormView. Invoked only once with true in ComponentWithFormTrait::submitFormOnRender() to synchronize FormView and FormInstance
@Mauriceu
Copy link
Author

Recreating the FormView may lead to unwanted behavior regarding the isValidated and validatedFields properties, as they will be set according to the old FormView. However, an issue may only arise when the underlying Code manually changes the value of a given Form/FormChild, which is discouraged anyway after its been submitted.

Also, for performance reasons I would like to avoid adding a second validation.

@Mauriceu Mauriceu force-pushed the 2.x branch 5 times, most recently from 03a4ee6 to 319f885 Compare February 25, 2025 16:17
…reation in case "shouldAutoSubmitForm" is true
@Mauriceu Mauriceu changed the title Fixes #1981 - ComponentWithFormTrait: Force recreation of FormView on PreReRender hook to synchronize FormInstance and FormView. [ComponentWithFormTrait] Force recreation of FormView on PreReRender hook to synchronize FormInstance and FormView. Feb 27, 2025
@Kocal
Copy link
Member

Kocal commented Feb 28, 2025

It makes sense to me, WDYT @smnandre?

@Kocal Kocal changed the title [ComponentWithFormTrait] Force recreation of FormView on PreReRender hook to synchronize FormInstance and FormView. [LiveComponent] [ComponentWithFormTrait] Force recreation of FormView on PreReRender hook to synchronize FormInstance and FormView. Feb 28, 2025
@smnandre
Copy link
Member

smnandre commented Mar 5, 2025

I'm not 100% sure to be honest, looks to me this could have side effects, especially with recent changes.

I will have a deeper look this week-end.

Also tests here focus only on errors, and not on data. But the added comment states "Recreate the FormView because it has been created by the submitForm() with a FormInterface whose values may have changed".

@Mauriceu
Copy link
Author

Mauriceu commented Mar 5, 2025

Also tests here focus only on errors, and not on data.

The data in this case would be the string manually added form error existing in the template, as it was added by the LiveAction.
But I'd gladly add more coverage as needed. This is my first official pull request so I am still getting the hang of it.

@smnandre
Copy link
Member

smnandre commented Mar 5, 2025

This is my first official pull request so I am still getting the hang of it.

Welcome aboard :)

The data in this case would be the string manually added form error existing in the template

Yes but this change will be executed for every component using this trait, on every re-render.. so there may be other cases :)

@Mauriceu
Copy link
Author

Mauriceu commented Mar 5, 2025

Thank you! Finally able to pay some of it back \o/

The added change will force the recreation of the formView only in cases where the shouldAutoSubmitForm property is set to false.
As I understood that could only happen in one of two cases:

  1. submitForm() was called before the PreReRender hook is invoked.
  2. Manually setting the shouldAutoSubmitForm property to false.

If necessary due to performance implications we could always introduce a function recreateFormView to give the User more control.

Another thought: Is it even necessary to create the formView inside the submitForm() function? We could extract the formValues directly from the instance, and create the formView last when the PreReRender hook is invoked.

@Mauriceu
Copy link
Author

Could theoretically potentially maybe also create a wrapper class for the FormInterface tracking all changes. However, I feel like that creates way too much overhead for what this PR is actually trying to solve...manual re-creation of the form is probably the option with the least impact on performance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LiveComponent] How do I manually add a FormError to the Form when my submit-function fails?
4 participants