diff --git a/.changeset/fresh-bikes-clap.md b/.changeset/fresh-bikes-clap.md new file mode 100644 index 000000000..125c722ee --- /dev/null +++ b/.changeset/fresh-bikes-clap.md @@ -0,0 +1,5 @@ +--- +'@tanstack/form-core': patch +--- + +respect dontValidate option in formApi array modifiers diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 6afa0fdc9..88d5ac9c5 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -2301,13 +2301,18 @@ export class FormApi< mergeOpts(options, { dontValidate: true }), ) - // Validate the whole array + all fields that have shifted - await this.validateField(field, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // Validate the whole array + all fields that have shifted + await this.validateField(field, 'change') + } // Shift down all meta after validating to make sure the new field has been mounted metaHelper(this).handleArrayFieldMetaShift(field, index, 'insert') - await this.validateArrayFieldsStartingFrom(field, index, 'change') + if (!dontValidate) { + await this.validateArrayFieldsStartingFrom(field, index, 'change') + } } /** @@ -2331,9 +2336,12 @@ export class FormApi< mergeOpts(options, { dontValidate: true }), ) - // Validate the whole array + all fields that have shifted - await this.validateField(field, 'change') - await this.validateArrayFieldsStartingFrom(field, index, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // Validate the whole array + all fields that have shifted + await this.validateField(field, 'change') + await this.validateArrayFieldsStartingFrom(field, index, 'change') + } } /** @@ -2368,9 +2376,12 @@ export class FormApi< this.deleteField(start as never) } - // Validate the whole array + all fields that have shifted - await this.validateField(field, 'change') - await this.validateArrayFieldsStartingFrom(field, index, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // Validate the whole array + all fields that have shifted + await this.validateField(field, 'change') + await this.validateArrayFieldsStartingFrom(field, index, 'change') + } } /** @@ -2395,11 +2406,14 @@ export class FormApi< // Swap meta metaHelper(this).handleArrayFieldMetaShift(field, index1, 'swap', index2) - // Validate the whole array - this.validateField(field, 'change') - // Validate the swapped fields - this.validateField(`${field}[${index1}]` as DeepKeys, 'change') - this.validateField(`${field}[${index2}]` as DeepKeys, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // Validate the whole array + this.validateField(field, 'change') + // Validate the swapped fields + this.validateField(`${field}[${index1}]` as DeepKeys, 'change') + this.validateField(`${field}[${index2}]` as DeepKeys, 'change') + } } /** @@ -2424,11 +2438,14 @@ export class FormApi< // Move meta between index1 and index2 metaHelper(this).handleArrayFieldMetaShift(field, index1, 'move', index2) - // Validate the whole array - this.validateField(field, 'change') - // Validate the moved fields - this.validateField(`${field}[${index1}]` as DeepKeys, 'change') - this.validateField(`${field}[${index2}]` as DeepKeys, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // Validate the whole array + this.validateField(field, 'change') + // Validate the moved fields + this.validateField(`${field}[${index1}]` as DeepKeys, 'change') + this.validateField(`${field}[${index2}]` as DeepKeys, 'change') + } } /** @@ -2457,8 +2474,11 @@ export class FormApi< } } - // validate array change - this.validateField(field, 'change') + const dontValidate = options?.dontValidate ?? false + if (!dontValidate) { + // validate array change + this.validateField(field, 'change') + } } /**