You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constform=useForm({defaultValues: {},onSubmit: ({ value })=>{},// <--- When I say "onSubmit" I refer to this guy herevalidators: {onSubmit: ({ value })=>{}// <--- When I say "validators.onSubmit" I refer to this guy here}});
It is a rare thing for server API implementors to expose an endpoint strictly dedicated to validation / verification of data.
validators.onSubmit is very convenient but, in practice, we'd almost always just POST the data to an endpoint and trust that it'll respond with a 400 and some details in the response payload.
So it'd be good to have a mean to handle a failed request that was fired off in onSubmit.
This currently works:
constform=useForm({defaultValues: {age: 0,name: '',email: '',},onSubmit: ({value: formData})=>{createUser.mutateAsync(formData).then(()=>form.reset()).catch((error: ApiError)=>{leterrorMessage='Unable to create user';if(error.message==='Conflict'){errorMessage='User already exists';}// This backend's validation relies on https://github.com/standard-schema/standard-schemaif(Array.isArray(error.details)){error.details.forEach((detail)=>{constfield=detail.path[0]askeyoftypeofformData;// this is 1 way to do it, currently:form.setFieldMeta(field,{errorMap: {onSubmit: detail.message,},errors: [],isBlurred: false,isDirty: true,isPristine: false,isTouched: true,isValidating: false,});});}// combined with this:form.setErrorMap({onSubmit: errorMessage,});});},});
With that we get both a form-level error and field-level errors, great! But it's quirky...setFieldMeta forces us (TypeScript) to specify everything, the errors: [] is completely ignored and only errorMap: {} is effective.
It'd be a better experience if we could do this instead:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This feature request is based off discussion 716 here.
Clarification:
It is a rare thing for server API implementors to expose an endpoint strictly dedicated to validation / verification of data.
validators.onSubmit
is very convenient but, in practice, we'd almost always just POST the data to an endpoint and trust that it'll respond with a400
and some details in the response payload.So it'd be good to have a mean to handle a failed request that was fired off in
onSubmit
.This currently works:
With that we get both a
form-level
error andfield-level
errors, great! But it's quirky...setFieldMeta
forces us (TypeScript) to specify everything, theerrors: []
is completely ignored and onlyerrorMap: {}
is effective.It'd be a better experience if we could do this instead:
This would feel very intuitive, especially since we can see that the type of
state.errorMap.onSubmit
isin a
<form.Subscribe selector={(state) => [state.errorMap.onSubmit]}></form.Subscribe>
.Beta Was this translation helpful? Give feedback.
All reactions