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

Revalidate (or at least clean) server errors on input change #126

Merged
merged 2 commits into from
Dec 13, 2022

Conversation

diogob
Copy link
Contributor

@diogob diogob commented Dec 9, 2022

Purpose

If we get an error coming from the server action and render it as a field error, it's safe to assume that upon changing that field value we should cease to see the error.
Since an error depends on the field and its value, and changing the value turns the error to an unknown state.

This should address #111.

Note that with the current shape of this PR is that we can't really revalidate unless we resubmit the form, and doing this on behalf of the user would be reckless at best.

It seems safer to clean the error state and allow the user to resubmit to get a new validation from the server.

TODO:

  • Implement a test case

@netlify
Copy link

netlify bot commented Dec 9, 2022

Deploy Preview for remix-forms ready!

Name Link
🔨 Latest commit da2d681
🔍 Latest deploy log https://app.netlify.com/sites/remix-forms/deploys/6397ab73feccad0009f90a9d
😎 Deploy Preview https://deploy-preview-126--remix-forms.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@danielweinmann
Copy link
Contributor

This approach LGTM 💪🏼

…other branch when errors are set on the server only. This will allow for client-side cleaning of server generated errors (though we can't really revalidate without submitting).
@diogob diogob marked this pull request as ready for review December 12, 2022 22:30
@diogob diogob merged commit 0a8a9a3 into main Dec 13, 2022
@diogob diogob deleted the revalidation-after-submission branch December 13, 2022 16:59
danielweinmann referenced this pull request Dec 13, 2022
@@ -262,9 +262,9 @@ function createForm({

const fieldErrors = (key: keyof SchemaType) => {
const message = (formErrors[key] as unknown as FieldError)?.message
return (message && [message]) || (errors && errors[key])
return browser() ? (message && [message]) : (errors && errors[key])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the case when the user sets the errors prop on the Form?
I think it will both be in the browser and have this errors[key]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipefreitag, in that case, we would have set the form error in the client here. We get the error from the prop, action data or fetcher data and set it on the client.

The only reason we still return (errors && errors[key]) is for when JS is disabled.

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

Successfully merging this pull request may close these issues.

4 participants