@@ -435,7 +435,10 @@ npm install @hookform/resolvers
435435- A resolver can not be used with the built-in validators (e.g.: required, min, etc.)
436436- When building a custom resolver:
437437 - Make sure that you return an object with both ` values ` and ` errors ` properties. Their default values should be an empty object. For example: ` {} ` .
438- - The keys of the ` error ` object should match the ` name ` values of your fields.
438+ - The keys of the ` errors ` object should match the ` name ` values of your fields, but they _ must_ be hierarchical rather than a single key for deep errors:
439+ ` ❌ { "participants.1.name": someErr } ` will not set or clear properly - instead, use ` ✅ { participants: [null, { name: someErr } ] } ` as this is reachable
440+ as ` errors.participants[1].name ` - you can still prepare your errors using flat keys, and then use a function like this one from the zod resolver:
441+ [ toNestErrors(flatErrs, resolverOptions)] ( https://github.com/react-hook-form/resolvers/blob/master/src/toNestErrors.ts )
439442
440443</Admonition >
441444
@@ -445,16 +448,11 @@ npm install @hookform/resolvers
445448
446449<TabGroup buttonLabels = { [" Yup" ," Zod" ," Joi" ," Ajv" ," Vest" , " Custom" ]} >
447450
448- ``` typescript copy sandbox="https://codesandbox.io/s/react-hook-form-apply-validation-ts-forked-nmbyh"
451+ ``` tsx copy sandbox="https://codesandbox.io/s/react-hook-form-apply-validation-ts-forked-nmbyh"
449452import { useForm } from " react-hook-form"
450453import { yupResolver } from " @hookform/resolvers/yup"
451454import * as yup from " yup"
452455
453- type Inputs = {
454- name: string
455- age: string
456- }
457-
458456const schema = yup
459457 .object ()
460458 .shape ({
@@ -464,7 +462,7 @@ const schema = yup
464462 .required ()
465463
466464const App = () => {
467- const { register, handleSubmit } = useForm < Inputs > ({
465+ const { register, handleSubmit } = useForm ({
468466 resolver: yupResolver (schema ), // yup, joi and even your own.
469467 })
470468
@@ -478,7 +476,7 @@ const App = () => {
478476}
479477```
480478
481- ``` typescript copy sandbox="https://codesandbox.io/s/react-hook-form-zod-resolver-ts-example-forked-w72vp"
479+ ``` tsx copy sandbox="https://codesandbox.io/s/react-hook-form-zod-resolver-ts-example-forked-w72vp"
482480import { useForm } from " react-hook-form"
483481import { zodResolver } from " @hookform/resolvers/zod"
484482import * as z from " zod"
@@ -491,16 +489,17 @@ const schema = z.object({
491489type Schema = z .infer <typeof schema >
492490
493491const App = () => {
494- const { register, handleSubmit } = useForm < Schema > ({
492+ const { register, handleSubmit } = useForm ({
495493 resolver: zodResolver (schema ),
496494 })
497495
498- const onSubmit = (data : Schema ) => {
499- console .log (data )
500- }
501-
502496 return (
503- < form onSubmit = {handleSubmit(onSubmit )}>
497+ <form
498+ onSubmit = { handleSubmit ((data ) => {
499+ // handle inputs
500+ console .log (data )
501+ })}
502+ >
504503 <input { ... register (" name" )} />
505504 <input { ... register (" age" , { valueAsNumber: true })} type = " number" />
506505 <input type = " submit" />
@@ -509,40 +508,44 @@ const App = () => {
509508}
510509```
511510
512- ``` typescript copy sandbox="https://codesandbox.io/s/react-hook-form-joiresolver-v6-ts-forked-5pseh"
513- import { useForm } from " react-hook-form" ;
514- import { joiResolver } from " @hookform/resolvers/joi" ;
515- import Joi from " joi" ;
511+ ``` tsx copy sandbox="https://codesandbox.io/s/react-hook-form-joiresolver-v6-ts-forked-5pseh"
512+ import { useForm } from " react-hook-form"
513+ import { joiResolver } from " @hookform/resolvers/joi"
514+ import Joi from " joi"
516515
517516interface IFormInput {
518- name: string ;
519- age: number ;
517+ name: string
518+ age: number
520519}
521520
522521const schema = Joi .object ({
523522 name: Joi .string ().required (),
524- age: Joi .number ().required ()
525- });
523+ age: Joi .number ().required (),
524+ })
526525
527526const App = () => {
528- const { register, handleSubmit, formState : { errors } } = useForm <IFormInput >({
529- resolver: joiResolver (schema )
530- });
527+ const {
528+ register,
529+ handleSubmit,
530+ formState : { errors },
531+ } = useForm <IFormInput >({
532+ resolver: joiResolver (schema ),
533+ })
531534 const onSubmit = (data : IFormInput ) => {
532- console .log (data );
533- };
535+ console .log (data )
536+ }
534537
535538 return (
536539 <form onSubmit = { handleSubmit (onSubmit )} >
537- < input {... register ("name "} / >
538- < input type = " number" {... register ("age "} / >
540+ <input { ... register (" name" ) } />
541+ <input type = " number" { ... register (" age" ) } />
539542 <input type = " submit" />
540543 </form >
541- );
544+ )
542545}
543546```
544547
545- ``` javascript copy sandbox="https://codesandbox.io/s/react-hook-form-ajvresolver-vr3imc"
548+ ``` tsx copy sandbox="https://codesandbox.io/s/react-hook-form-ajvresolver-vr3imc"
546549import { useForm } from " react-hook-form"
547550import { ajvResolver } from " @hookform/resolvers/ajv"
548551
@@ -586,7 +589,7 @@ const App = () => {
586589}
587590```
588591
589- ``` javascript copy sandbox="https://codesandbox.io/s/vest-8q874"
592+ ``` tsx copy sandbox="https://codesandbox.io/s/vest-8q874"
590593import * as React from " react"
591594import { useForm } from " react-hook-form"
592595import { vestResolver } from " @hookform/resolvers/vest"
@@ -633,7 +636,7 @@ const App = () => {
633636}
634637```
635638
636- ``` typescript copy sandbox="https://codesandbox.io/s/react-hook-form-customresoliver-ts-v7-juc63"
639+ ``` tsx copy sandbox="https://codesandbox.io/s/react-hook-form-customresoliver-ts-v7-juc63"
637640import * as React from " react"
638641import { useForm } from " react-hook-form"
639642import * as Joi from " joi"
0 commit comments