Skip to content
New issue

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

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

Already on GitHub? # to your account

Implement clearErrors on isXmlHttpRequest #2

Open
mpiot opened this issue Sep 8, 2023 · 2 comments
Open

Implement clearErrors on isXmlHttpRequest #2

mpiot opened this issue Sep 8, 2023 · 2 comments

Comments

@mpiot
Copy link

mpiot commented Sep 8, 2023

In case the dependent field has validator on it (like NotNull, NotBlank) an error should appear because the field is added but is empty.
To fix it, we commonly add this in the Controller:

   if ($request->isXmlHttpRequest() && $form instanceof ClearableErrorsInterface) {
        $form->clearErrors(true);
    }

Maybe is it possible, to retrieve the Request inside the DynamicFormBuilder and check it automatically ?

@weaverryan
Copy link
Contributor

I think isXmlHttpRequest() is not going to be a good enough signal to use inside the library - fetch(), for example, does not send the X-Requested-With header that this relies on. We would need some... extra signal to know when to clear - like some method on DynamicFormBuilder where you configure that. But I think it's outside of the scope of what this library should be doing. However, enhancing the documentation to show a real-world example (including this clearing) to the docs would be AWESOME... if you want to open a PR ;)

@mpiot
Copy link
Author

mpiot commented Sep 22, 2023

Yep, I've think about isXmlHttpRequest() and default support it because of usage with the Symfony Form and Symfony Stimulus/Turbo that, I believe, defaultly use XmlRequests.

But, actually in all dependant form I do I put that fragment, then I can see to create a PR that just mention it as a reminder for user.

if ($request->isXmlHttpRequest() && $form instanceof ClearableErrorsInterface) {
    $form->clearErrors(true);
}

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

No branches or pull requests

2 participants