Handle global/server errors in forms #9691
Replies: 5 comments 12 replies
-
This would be extremely helpful, specially with the new next 13. Few questions to think about
|
Beta Was this translation helpful? Give feedback.
-
[Brainstorm]: What if errors were split or merged based on the value of an setError('registerInput', { ..., message: 'custom message' });
{errors.registerInput.message}
setError('registerInput', { ..., message: 'server message' }, { isServer?: boolean = false, meta?: object }); // statusCode, etc
{errors.server.message}
{errors.server?.meta?.statusCode} // for additional data or UI validation So only when |
Beta Was this translation helpful? Give feedback.
-
What I haven't understood yet: Where is the naming (and therefore the typesafe usage) of the error fields (e.g. "random" from For such cases, I right now create variables to save strings that define an error field name. This is especially important when using RHF with Zod, so you can easily check additional form state to show more specific error messages. One way to integrate this check is by using Zod's const schemaUpdatePost = z
.object({
postId: z.string().cuid(),
title: z.string().min(1).optional(),
subtitle: z.string().optional(),
})
.refine((data) => !!data.title || !!data.subtitle, {
message: 'Either title or subtitle should be changed.',
// 🟠 this is the field name to show an error for
path: ["foo"],
}) For this error to be integrated with RHF, you would usually set your custom error yourself, something like the example below. Saving the error field name in a variable prevents RHF and Zod to be out of sync: const generalFormErrorKey = 'general-form-error-key'
const schemaUpdatePost = z
.object(...)
.refine((data) => !!data.title || !!data.subtitle, {
...
// 🟠 use variable
path: [generalFormErrorKey],
})
// usage with RHF
const {
register,
formState: {
errors: {
// @ts-expect-error This error was added via Zod, but RHF's type is not aware of it.
[generalFormErrorKey]: generalErrorRaw,
},
},
} = useForm<UserUpdate>({ ... }) As you can see, this is not typesafe right now (-> |
Beta Was this translation helpful? Give feedback.
-
I'm not pretty sure @enyelsequeira's questions was the same meaning with this;
Or do you have any solution to this or do you think global errors can implement this use case? @bluebill1049 |
Beta Was this translation helpful? Give feedback.
-
Any update on this @bluebill1049 ? as for now
developer needs to manually remove that server error before the next submission could pass, |
Beta Was this translation helpful? Give feedback.
-
💁♂️ Summary
This proposal is made so we can discuss and find a proper and better to handle generic errors which not associated with specific form fields.
👋🏻 Motivation
There are errors which potentially not associated with the form field or coming from the server, we would like a better way to handle it to provide a better developer experience.
👀 Related feature request: #7661
The most user would have used
setError
to set server error:This works relatively well, however, it will require a developer to manually remove that server error before the next submission could pass, because the server error does not associate with any input, so the validation error will be persister unless the user invoked
clearErrors('server')
. eg:📖 Design/Proposal
Include a global root error, which is:
clearErrors
root
for consistency as we used for field array root errors as well😢 Drawbacks
Potentially naming clashes with an existing error that users are already using.
🙋♂️ Unresolved questions
Do we have any better approach?
cc @Moshyfawn @leapful
Beta Was this translation helpful? Give feedback.
All reactions