@@ -49,6 +49,10 @@ interface FieldArrayRegistry {
49
49
} ;
50
50
}
51
51
52
+ interface ValidateHandlerOptions {
53
+ onlyBlurred ?: boolean ;
54
+ }
55
+
52
56
export interface FormSubmitHelper < Values extends FormValues > {
53
57
setSubmitting : ( isSubmitting : boolean ) => void ;
54
58
readonly initialValues : Values ;
@@ -262,7 +266,7 @@ export function useForm<
262
266
} ) ;
263
267
264
268
return validateTiming . value === 'blur'
265
- ? runAllValidateHandler ( state . values )
269
+ ? runAllValidateHandler ( state . values , { onlyBlurred : true } )
266
270
: Promise . resolve ( ) ;
267
271
} ;
268
272
@@ -503,20 +507,50 @@ export function useForm<
503
507
} ) ;
504
508
} ;
505
509
506
- const runAllValidateHandler = ( values : Values = state . values ) => {
510
+ /**
511
+ * Creates a new object of errors, but only with the errors of touched fields.
512
+ *
513
+ * @param errors The union of field and form errors
514
+ * @returns The field errors that have been touched
515
+ */
516
+ const getTouchedErrors = ( errors : FormErrors < Values > ) => {
517
+ const touchedFieldsKeys : any = [ ] ;
518
+
519
+ Object . entries ( state . touched . value ) . forEach ( ( [ key , isTouched ] ) => {
520
+ if ( isTouched ) {
521
+ touchedFieldsKeys . push ( key ) ;
522
+ }
523
+ } ) ;
524
+
525
+ const touchedErrorsEntries : Array < [ keyof FormErrors < Values > , string ] > =
526
+ Object . entries ( errors ) . filter ( ( [ key ] ) => {
527
+ return touchedFieldsKeys . includes ( key ) ;
528
+ } ) ;
529
+
530
+ return Object . fromEntries ( touchedErrorsEntries ) as FormErrors < Values > ;
531
+ } ;
532
+
533
+ const runAllValidateHandler = (
534
+ values : Values = state . values ,
535
+ validateOptions ?: ValidateHandlerOptions ,
536
+ ) => {
507
537
dispatch ( { type : ACTION_TYPE . SET_ISVALIDATING , payload : true } ) ;
508
538
return Promise . all ( [
509
539
runFieldValidateHandler ( values ) ,
510
540
options . validate ? runValidateHandler ( values ) : { } ,
511
541
] )
512
542
. then ( ( [ fieldErrors , validateErrors ] ) => {
513
- const errors = deepmerge . all < FormErrors < Values > > (
543
+ const baseErrors = deepmerge . all < FormErrors < Values > > (
514
544
[ fieldErrors , validateErrors ] ,
515
545
{
516
546
arrayMerge,
517
547
} ,
518
548
) ;
519
549
550
+ const errors = validateOptions ?. onlyBlurred
551
+ ? getTouchedErrors ( baseErrors )
552
+ : baseErrors ;
553
+
520
554
setErrors ( errors ) ;
521
555
522
556
return errors ;
0 commit comments