How to return custom data/error on action #7
-
currently i have this on my action function,
im just trying to simulate a scenario where in i need to do a custom validation on the action function, but when i return a new property i get an error on useActionData.
how do i resolve this? or what is the best way to deal with this scenario? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think your main issue here @ronkristoff is that your export const action = async ({ request }: ActionArgs) => {
const {
errors,
data,
receivedValues: defaultValues,
} = await getValidatedFormData(request, resolver);
if (errors) {
return json({ errors, defaultValues });
}
//return some data that comes from custom validation
return json({ customErrors: "test" }); is returning either errors and defaultValues or customErrors, if you try doing it like this: export const action = async ({ request }: ActionArgs) => {
const {
errors,
data,
receivedValues: defaultValues,
} = await getValidatedFormData(request, resolver);
if (errors) {
return json({ errors, defaultValues, customErrors: "" });
}
//return some data that comes from custom validation
return json({ customErrors: "test" }); it should make typescript happy because you do not return the customErrors on every request typescript can't guarantee that it will be there |
Beta Was this translation helpful? Give feedback.
-
On the topic of errors, should custom errors conform to the { fieldName: { message: string, type?: string } } type to be added as errors, since this is the error pattern from react-hook-forms? |
Beta Was this translation helpful? Give feedback.
I think your main issue here @ronkristoff is that your
is returning either errors and defaultValues or customErrors, if you try doing it like this: