onServerValidate example lacking other patterns.
I don't understand why we have formErrors.map but on onServerValidate but the example only returns 1 error message.
Is there a way to output it in a field? It would be great to have an example where we validate each field in server validation and then output the field errors.
An example also with standard schema validate with custom db validation logic.
8 Replies
conscious-sapphire•4mo ago
This is something I would like also.
I can help a little, now. The server validation happens on submission. The return can be anything, including a simple string, but if you wanted to check several things (eg, a password fitting the rules) then you might return several issues. After that, the response should be merged to formErrors with the useActionState, which means client-side checks that might have left messages will also be present. The map allows you to show them all.
fair-roseOP•4mo ago
I have ditched the createServerValidate altogether and used next-safe-actions.
create-form.tsx
_action.ts
_validation-schema.ts
magic-amber•4mo ago
https://github.com/TanStack/form/pull/1432
I'm just finishing some things up here
GitHub
feat (form-core): Merging Form-level server errors with Field-level...
Instead of the previous serverValidate function just returning a string, we now return an object:
const serverValidate = createServerValidate({
...formOpts,
onServerValidate: ({ value }) =&...
conscious-sapphire•4mo ago
This is also what I'm doing.
foreign-sapphire•4mo ago
formatValidationErrors
what is this function sr?
conscious-sapphire•4mo ago
import { formatValidationErrors } from "next-safe-action";
fair-roseOP•3mo ago
It's a custom function I forgot to include in the initial message. I didn't use the one from next-safe-action. I'm not sure if they're identical. 😄
like-gold•3mo ago
Oh this is huge - thank you!
LMK if you need help getting it past the line 🙂