Skip to content

Is there a way to validate default values on initial render (before mount)? #1514

Answered by LeCarbonator
akkadaska asked this question in Q&A
Discussion options

You must be logged in to vote

As of version 1.11, you can set field errors from formApi.setErrorMap. See the documentation for setting field-level errors from the form

const form = useForm({
 // ...
})

form.setErrorMap({
   onMount: {
      form: 'Mount errors set directly',
      fields: {
        'nested.field.error': 'An error'
      }
   }
})

If your validators are standard schemas, there is a helper function to help you format it:

const schema = z.object({ /* ... */ });

const form = useForm({
 // ...
});

form.setErrorMap({
  onMount: form.parseValuesWithSchema(schema)
})

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@akkadaska
Comment options

@LeCarbonator
Comment options

@akkadaska
Comment options

@akkadaska
Comment options

Answer selected by akkadaska
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants