@@ -1545,6 +1545,23 @@ export class FormApi<
15451545 return fieldErrorMapMap . flat ( )
15461546 }
15471547
1548+ /**
1549+ * @private
1550+ */
1551+ collectArrayFields = < TField extends DeepKeysOfType < TFormData , any [ ] > > (
1552+ field : TField ,
1553+ index : number ,
1554+ ) => {
1555+ const fieldKeysToCollect = [ `${ field } [${ index } ]` ]
1556+
1557+ // We also have to include all fields that are nested in the array fields
1558+ const fieldsToCollect = Object . keys ( this . fieldInfo ) . filter ( ( fieldKey ) =>
1559+ fieldKeysToCollect . some ( ( key ) => fieldKey . startsWith ( key ) ) ,
1560+ ) as DeepKeys < TFormData > [ ]
1561+
1562+ return fieldsToCollect
1563+ }
1564+
15481565 /**
15491566 * Validates the children of a specified array in the form starting from a given index until the end using the correct handlers for a given validation type.
15501567 */
@@ -1561,16 +1578,32 @@ export class FormApi<
15611578 ? Math . max ( ( currentValue as Array < unknown > ) . length - 1 , 0 )
15621579 : null
15631580
1564- // We have to validate all fields that have shifted (at least the current field)
1565- const fieldKeysToValidate = [ ` ${ field } [ ${ index } ]` ]
1566- for ( let i = index + 1 ; i <= ( lastIndex ?? 0 ) ; i ++ ) {
1567- fieldKeysToValidate . push ( ` ${ field } [ ${ i } ]` )
1581+ const fieldsToValidate : DeepKeys < TFormData > [ ] = [ ]
1582+ for ( let i = index ; i <= ( lastIndex ?? 0 ) ; i ++ ) {
1583+ const collectedFields = this . collectArrayFields ( field , i )
1584+ fieldsToValidate . push ( ... collectedFields )
15681585 }
15691586
1570- // We also have to include all fields that are nested in the shifted fields
1571- const fieldsToValidate = Object . keys ( this . fieldInfo ) . filter ( ( fieldKey ) =>
1572- fieldKeysToValidate . some ( ( key ) => fieldKey . startsWith ( key ) ) ,
1573- ) as DeepKeys < TFormData > [ ]
1587+ // Validate the fields
1588+ const fieldValidationPromises : Promise < ValidationError [ ] > [ ] = [ ] as any
1589+ batch ( ( ) => {
1590+ fieldsToValidate . forEach ( ( nestedField ) => {
1591+ fieldValidationPromises . push (
1592+ Promise . resolve ( ) . then ( ( ) => this . validateField ( nestedField , cause ) ) ,
1593+ )
1594+ } )
1595+ } )
1596+
1597+ const fieldErrorMapMap = await Promise . all ( fieldValidationPromises )
1598+ return fieldErrorMapMap . flat ( )
1599+ }
1600+
1601+ validateArrayFields = async < TField extends DeepKeysOfType < TFormData , any [ ] > > (
1602+ field : TField ,
1603+ index : number ,
1604+ cause : ValidationCause ,
1605+ ) => {
1606+ const fieldsToValidate = this . collectArrayFields ( field , index )
15741607
15751608 // Validate the fields
15761609 const fieldValidationPromises : Promise < ValidationError [ ] > [ ] = [ ] as any
0 commit comments