@@ -366,6 +366,92 @@ describe('form api', () => {
366366 expect ( form . getFieldValue ( 'names' ) ) . toStrictEqual ( [ 'one' , 'two' , 'three' ] )
367367 } )
368368
369+ it ( "should clean onMount errors when replacing an array field's value" , async ( ) => {
370+ const form = new FormApi ( {
371+ defaultValues : {
372+ people : [
373+ {
374+ firstName : '' ,
375+ lastName : '' ,
376+ } ,
377+ ] ,
378+ } ,
379+ validators : {
380+ onMount : ( { value } ) => {
381+ let fieldErrors : Record < string , string > = { }
382+
383+ value . people . map ( ( x , index ) => {
384+ if ( x . firstName . length < 3 ) {
385+ fieldErrors [ `people[${ index } ].firstName` ] =
386+ 'First name is too short'
387+ }
388+
389+ if ( x . lastName . length < 3 ) {
390+ fieldErrors [ `people[${ index } ].lastName` ] =
391+ 'Last name is too short'
392+ }
393+ } )
394+
395+ if ( Object . keys ( fieldErrors ) . length > 0 ) {
396+ return {
397+ fields : { ...fieldErrors } ,
398+ }
399+ }
400+
401+ return fieldErrors
402+ } ,
403+ onChange : ( { value } ) => {
404+ let fieldErrors : Record < string , string > = { }
405+
406+ value . people . map ( ( x , index ) => {
407+ if ( x . firstName . length < 3 ) {
408+ fieldErrors [ `people[${ index } ].firstName` ] =
409+ 'First name is too short'
410+ }
411+
412+ if ( x . lastName . length < 3 ) {
413+ fieldErrors [ `people[${ index } ].lastName` ] =
414+ 'Last name is too short'
415+ }
416+ } )
417+
418+ if ( Object . keys ( fieldErrors ) . length > 0 ) {
419+ return {
420+ fields : { ...fieldErrors } ,
421+ }
422+ }
423+
424+ return fieldErrors
425+ } ,
426+ } ,
427+ } )
428+ form . mount ( )
429+
430+ // Since validation runs through the field, a field must be mounted for that array
431+ new FieldApi ( { form, name : 'people' } ) . mount ( )
432+
433+ await form . replaceFieldValue ( 'people' , 0 , {
434+ firstName : 'Chuck' ,
435+ lastName : 'Norris' ,
436+ } )
437+
438+ expect ( form . state . values ) . toStrictEqual ( {
439+ people : [
440+ {
441+ firstName : 'Chuck' ,
442+ lastName : 'Norris' ,
443+ } ,
444+ ] ,
445+ } )
446+ expect . soft ( form . state . fieldMetaBase [ 'people' ] ! . errorMap ) . toStrictEqual ( { } )
447+ expect
448+ . soft ( form . state . fieldMetaBase [ 'people[0].firstName' ] ! . errorMap )
449+ . toStrictEqual ( { } )
450+ expect
451+ . soft ( form . state . fieldMetaBase [ 'people[0].firstName' ] ! . errorSourceMap )
452+ . toStrictEqual ( { } )
453+ } )
454+
369455 it ( "should run onChange validation when inserting an array field's value" , ( ) => {
370456 const form = new FormApi ( {
371457 defaultValues : {
0 commit comments