Is there a way to validate default values on initial render (before mount)? #1514
-
Hi, thank you for the great library! Currently, we can specify validation strategies such as This means that if default values are invalid, there's no way to show validation errors immediately without causing a flicker or layout shift. Using Is there a recommended way to validate default values during the initial render so that errors are already present in the server-rendered HTML? Or would it make sense to support an explicit Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
As of version 1.11, you can set field errors from 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)
}) |
Beta Was this translation helpful? Give feedback.
As of version 1.11, you can set field errors from
formApi.setErrorMap
. See the documentation for setting field-level errors from the formIf your validators are standard schemas, there is a helper function to help you format it: