Replies: 2 comments
-
|
The validation itself does run for every registered field on submit, the problem is on the display side. New array items start with The fix is to also show errors once the form has been submitted at least once. <form.Field
name={`people[${index}].name`}
validators={{ onChange: ({ value }) => !value ? 'Required' : undefined }}
>
{(field) => (
<>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
/>
{(field.state.meta.isTouched || form.state.submissionAttempts > 0) &&
field.state.meta.errors.length > 0 && (
<em>{field.state.meta.errors.join(', ')}</em>
)}
</>
)}
</form.Field>With that, the moment the user clicks Submit and the new empty items fail validation, their errors render without the user needing to focus each field. If you want a form-level error too ("at least one person required"), a validator on the form itself: const form = useAppForm({
defaultValues: { people: [] as Person[] },
validators: {
onSubmit: ({ value }) =>
value.people.length === 0 ? 'Add at least one person' : undefined,
},
});
|
Beta Was this translation helpful? Give feedback.
-
|
I found the solution was adding const form = useForm({
canSubmitWhenInvalid: true,
...
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an form with an array using dynamic validation. When I add a new empty array entry and click submit, I see a validation error indicating that the field is empty as expected. If I add an additional couple of array entries and click submit again, validation errors don't show for the additional entries unless I fix the validation issue with the first entry.
See this reproduction:
https://stackblitz.com/edit/tanstack-form-x6aimpfn
Is there any way to trigger validation for all entries when submit is clicked?
Beta Was this translation helpful? Give feedback.
All reactions