T
TanStack•13mo ago
fair-rose

Manually modify the error state of the field

I want to initiate a request after passing a form validation. I can only know which form fields are illegal after the request is returned. Is it possible to modify the field status?
No description
5 Replies
inland-turquoise
inland-turquoise•13mo ago
You need to modify the errorMap, not errors
fair-rose
fair-roseOP•13mo ago
@crutchcorn Can you give me a code example? I feel a little stuck.😭 😭
inland-turquoise
inland-turquoise•13mo ago
S'all good 🙂 I'm not avail to give a full code sample at the moment (CC @Leonardo might be able to help) but will get back to you later today if nothing else
fair-rose
fair-roseOP•13mo ago
Many thanks🥹
ratty-blush
ratty-blush•13mo ago
This seems like a good use case to extend our FieldValidateFn to also take an async function, then you could use await mutateAsync inside a validator and return strings so that the form will handle where to put them (inside errorMap) 🤔 Anyway @awn with this snippet you can set errors manually and you will find them already into the errors array of a form or a field
form.setErrorMap({
onSubmit: 'Registration failed',
});

form.setFieldMeta('account', (meta) => ({
...meta,
errorMap: {
onSubmit: `Username ${value.account} is taken`,
},
}));
form.setErrorMap({
onSubmit: 'Registration failed',
});

form.setFieldMeta('account', (meta) => ({
...meta,
errorMap: {
onSubmit: `Username ${value.account} is taken`,
},
}));

Did you find this page helpful?