Skip to content

Commit 33cce81

Browse files
PejmanNikPejman Nikramautofix-ci[bot]LeCarbonator
authored
fix(form-core): respect dontValidate option in form array methods (#1775)
* fix(form-core): respect dontValidate option in formApi array modifiers check the `dontValidate` option in array modifiers before applying validation fix #1764 * ci: apply automated fixes and generate docs * fix(form-core): change changeset to patch --------- Co-authored-by: Pejman Nikram <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: LeCarbonator <[email protected]>
1 parent e877dfb commit 33cce81

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

.changeset/fresh-bikes-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/form-core': patch
3+
---
4+
5+
respect dontValidate option in formApi array modifiers

packages/form-core/src/FormApi.ts

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,13 +2318,18 @@ export class FormApi<
23182318
mergeOpts(options, { dontValidate: true }),
23192319
)
23202320

2321-
// Validate the whole array + all fields that have shifted
2322-
await this.validateField(field, 'change')
2321+
const dontValidate = options?.dontValidate ?? false
2322+
if (!dontValidate) {
2323+
// Validate the whole array + all fields that have shifted
2324+
await this.validateField(field, 'change')
2325+
}
23232326

23242327
// Shift down all meta after validating to make sure the new field has been mounted
23252328
metaHelper(this).handleArrayFieldMetaShift(field, index, 'insert')
23262329

2327-
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2330+
if (!dontValidate) {
2331+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2332+
}
23282333
}
23292334

23302335
/**
@@ -2348,9 +2353,12 @@ export class FormApi<
23482353
mergeOpts(options, { dontValidate: true }),
23492354
)
23502355

2351-
// Validate the whole array + all fields that have shifted
2352-
await this.validateField(field, 'change')
2353-
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2356+
const dontValidate = options?.dontValidate ?? false
2357+
if (!dontValidate) {
2358+
// Validate the whole array + all fields that have shifted
2359+
await this.validateField(field, 'change')
2360+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2361+
}
23542362
}
23552363

23562364
/**
@@ -2385,9 +2393,12 @@ export class FormApi<
23852393
this.deleteField(start as never)
23862394
}
23872395

2388-
// Validate the whole array + all fields that have shifted
2389-
await this.validateField(field, 'change')
2390-
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2396+
const dontValidate = options?.dontValidate ?? false
2397+
if (!dontValidate) {
2398+
// Validate the whole array + all fields that have shifted
2399+
await this.validateField(field, 'change')
2400+
await this.validateArrayFieldsStartingFrom(field, index, 'change')
2401+
}
23912402
}
23922403

23932404
/**
@@ -2412,11 +2423,14 @@ export class FormApi<
24122423
// Swap meta
24132424
metaHelper(this).handleArrayFieldMetaShift(field, index1, 'swap', index2)
24142425

2415-
// Validate the whole array
2416-
this.validateField(field, 'change')
2417-
// Validate the swapped fields
2418-
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
2419-
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
2426+
const dontValidate = options?.dontValidate ?? false
2427+
if (!dontValidate) {
2428+
// Validate the whole array
2429+
this.validateField(field, 'change')
2430+
// Validate the swapped fields
2431+
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
2432+
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
2433+
}
24202434
}
24212435

24222436
/**
@@ -2441,11 +2455,14 @@ export class FormApi<
24412455
// Move meta between index1 and index2
24422456
metaHelper(this).handleArrayFieldMetaShift(field, index1, 'move', index2)
24432457

2444-
// Validate the whole array
2445-
this.validateField(field, 'change')
2446-
// Validate the moved fields
2447-
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
2448-
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
2458+
const dontValidate = options?.dontValidate ?? false
2459+
if (!dontValidate) {
2460+
// Validate the whole array
2461+
this.validateField(field, 'change')
2462+
// Validate the moved fields
2463+
this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
2464+
this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
2465+
}
24492466
}
24502467

24512468
/**
@@ -2474,8 +2491,11 @@ export class FormApi<
24742491
}
24752492
}
24762493

2477-
// validate array change
2478-
this.validateField(field, 'change')
2494+
const dontValidate = options?.dontValidate ?? false
2495+
if (!dontValidate) {
2496+
// validate array change
2497+
this.validateField(field, 'change')
2498+
}
24792499
}
24802500

24812501
/**

0 commit comments

Comments
 (0)