form icon indicating copy to clipboard operation
form copied to clipboard

[feat] Field validation from form schema

Open crutchcorn opened this issue 1 year ago • 2 comments

This PR introduces the ability to pass a schema to the validators object on a form and have it validate each specific field:

const form = useForm({
  defaultValues: {
    firstName: '',
    lastName: '',
  },
  onSubmit: async ({ value }) => {
    // Do something with form data
    console.log(value)
  },
  validators: {
    onChange: z.object({
      // Errors here are passed to the <form.Field name="firstName"> component
      firstName: z.string().min(3, 'First name must be at least 3 characters'),
      lastName: z.string().min(3, 'Last name must be at least 3 characters'),
    }),
  },
  // Add a validator to support Zod usage in Form and Field
  validatorAdapter: zodValidator({}),
})

TODO

  • [ ] Update Valibot adapter
  • [x] Update Yup adapter
  • [ ] Update docs

crutchcorn avatar Aug 28 '24 09:08 crutchcorn