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

Form is submitting on every re-render #40

Open
FrancoisCossette opened this issue Oct 18, 2024 · 3 comments
Open

Form is submitting on every re-render #40

FrancoisCossette opened this issue Oct 18, 2024 · 3 comments

Comments

@FrancoisCossette
Copy link

Using ComponentWithFormTrait, I notice that every re-render of the form submits it thanks to it's submitFormOnRender function and this seems to be intentionally mandatory.

I have some FormEvents that listen to Submit events to process the submitted data, and they are not liking this. Since I'm setting some fields as required, I expect them to be submitted with that, but now they are receiving some empty data on every re-render.

It seem to me that the only way to fix this is to make sure that my events can handle missing data, even when it's required data. Is this really the only way, or am I missing something?

@julian-koster
Copy link

julian-koster commented Nov 22, 2024

There are other options which are not recommended and bad practice (I included an example of such a workaround below), but the submitFormOnRender is mandatory when using ComponentWithFormTrait.
You could pre-fill data, render some part of the form in the livecomponent, other parts outside of it, etc. I've been using this bundle a lot over the past year and haven't found an easier workaround than (like you suggest) configuring your events to handle empty data.

I usually go this route:

public function onFormSubmit(FormEvent $event)
{
    $data = $event->getData();
    if (empty($data['requiredField'])) {
            $data['requiredField'] = 'someDefaulValue';
            $event->setData($data); 
    }
}

@sadikoff
Copy link

Hi @FrancoisCossette

it depends on what you want to achieve, for example, if you have a real button to submit data, you can disable auto submit and do it only when the button is pressed.

@juuuuln yeah that is probably a workaround, but it's totally not a good practice

@julian-koster
Copy link

@sadikoff, I should've mentioned that it isn't recommended and hacky. Will update my comment!

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

No branches or pull requests

3 participants