Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-bikes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/form-core': patch
---

respect dontValidate option in formApi array modifiers
62 changes: 41 additions & 21 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}

/**
Expand All @@ -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')
}
}

/**
Expand Down Expand Up @@ -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')
}
}

/**
Expand All @@ -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<TFormData>, 'change')
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, '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<TFormData>, 'change')
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
}
}

/**
Expand All @@ -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<TFormData>, 'change')
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, '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<TFormData>, 'change')
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
}
}

/**
Expand Down Expand Up @@ -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')
}
}

/**
Expand Down
Loading