-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathFormApi.ts
1161 lines (1050 loc) · 32.4 KB
/
FormApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Store } from '@tanstack/store'
import {
deleteBy,
functionalUpdate,
getAsyncValidatorArray,
getBy,
getSyncValidatorArray,
isNonEmptyArray,
setBy,
} from './utils'
import type { Updater } from './utils'
import type { DeepKeys, DeepValue } from './util-types'
import type { FieldApi, FieldMeta } from './FieldApi'
import type {
UpdateMetaOptions,
ValidationCause,
ValidationError,
ValidationErrorMap,
ValidationErrorMapKeys,
Validator,
} from './types'
/**
* @private
*/
export type FormValidateFn<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> = (props: {
value: TFormData
formApi: FormApi<TFormData, TFormValidator>
}) => ValidationError
/**
* @private
*/
export type FormValidateOrFn<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> =
TFormValidator extends Validator<TFormData, infer TFN>
? TFN
: FormValidateFn<TFormData, TFormValidator>
/**
* @private
*/
export type FormValidateAsyncFn<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> = (props: {
value: TFormData
formApi: FormApi<TFormData, TFormValidator>
signal: AbortSignal
}) => ValidationError | Promise<ValidationError>
/**
* @private
*/
export type FormAsyncValidateOrFn<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> =
TFormValidator extends Validator<TFormData, infer FFN>
? FFN | FormValidateAsyncFn<TFormData, TFormValidator>
: FormValidateAsyncFn<TFormData, TFormValidator>
export interface FormValidators<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> {
/**
* Optional function that fires as soon as the component mounts.
*/
onMount?: FormValidateOrFn<TFormData, TFormValidator>
/**
* Optional function that checks the validity of your data whenever a value changes
*/
onChange?: FormValidateOrFn<TFormData, TFormValidator>
/**
* Optional onChange asynchronous counterpart to onChange. Useful for more complex validation logic that might involve server requests.
*/
onChangeAsync?: FormAsyncValidateOrFn<TFormData, TFormValidator>
/**
* The default time in milliseconds that if set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds.
*/
onChangeAsyncDebounceMs?: number
/**
* Optional function that validates the form data when a field loses focus, returns a ValidationError
*/
onBlur?: FormValidateOrFn<TFormData, TFormValidator>
/**
* Optional onBlur asynchronous validation method for when a field loses focus return a `ValidationError` or a promise of `Promise<ValidationError>`
*/
onBlurAsync?: FormAsyncValidateOrFn<TFormData, TFormValidator>
/**
* The default time in milliseconds that if set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds.
*/
onBlurAsyncDebounceMs?: number
onSubmit?: FormValidateOrFn<TFormData, TFormValidator>
onSubmitAsync?: FormAsyncValidateOrFn<TFormData, TFormValidator>
}
/**
* @private
*/
export interface FormTransform<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> {
fn: (
formBase: FormApi<TFormData, TFormValidator>,
) => FormApi<TFormData, TFormValidator>
deps: unknown[]
}
/**
* An object representing the options for a form.
*/
export interface FormOptions<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> {
/**
* Set initial values for your form.
*/
defaultValues?: TFormData
/**
* The default state for the form.
*/
defaultState?: Partial<FormState<TFormData>>
/**
* If true, always run async validation, even when sync validation has produced an error. Defaults to undefined.
*/
asyncAlways?: boolean
/**
* Optional time in milliseconds if you want to introduce a delay before firing off an async action.
*/
asyncDebounceMs?: number
/**
* A validator adapter to support usage of extra validation types (IE: Zod, Yup, or Valibot usage)
*/
validatorAdapter?: TFormValidator
/**
* A list of validators to pass to the form
*/
validators?: FormValidators<TFormData, TFormValidator>
/**
* A function to be called when the form is submitted, what should happen once the user submits a valid form returns `any` or a promise `Promise<any>`
*/
onSubmit?: (props: {
value: TFormData
formApi: FormApi<TFormData, TFormValidator>
}) => any | Promise<any>
/**
* Specify an action for scenarios where the user tries to submit an invalid form.
*/
onSubmitInvalid?: (props: {
value: TFormData
formApi: FormApi<TFormData, TFormValidator>
}) => void
transform?: FormTransform<TFormData, TFormValidator>
}
/**
* An object representing the validation metadata for a field. Not intended for public usage.
*/
export type ValidationMeta = {
/**
* An abort controller stored in memory to cancel previous async validation attempts.
*/
lastAbortController: AbortController
}
/**
* An object representing the field information for a specific field within the form.
*/
export type FieldInfo<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> = {
/**
* An instance of the FieldAPI.
*/
instance: FieldApi<
TFormData,
any,
Validator<unknown, unknown> | undefined,
TFormValidator
> | null
/**
* A record of field validation internal handling.
*/
validationMetaMap: Record<ValidationErrorMapKeys, ValidationMeta | undefined>
}
/**
* An object representing the current state of the form.
*/
export type FormState<TFormData> = {
/**
* The current values of the form fields.
*/
values: TFormData
/**
* A boolean indicating if the form is currently validating.
*/
isFormValidating: boolean
/**
* A boolean indicating if the form is valid.
*/
isFormValid: boolean
/**
* The error array for the form itself.
*/
errors: ValidationError[]
/**
* The error map for the form itself.
*/
errorMap: ValidationErrorMap
/**
* An internal mechanism used for keeping track of validation logic in a form.
*/
validationMetaMap: Record<ValidationErrorMapKeys, ValidationMeta | undefined>
/**
* A record of field metadata for each field in the form.
*/
fieldMeta: Record<DeepKeys<TFormData>, FieldMeta>
/**
* A boolean indicating if any of the form fields are currently validating.
*/
isFieldsValidating: boolean
/**
* A boolean indicating if all the form fields are valid.
*/
isFieldsValid: boolean
/**
* A boolean indicating if the form is currently submitting.
*/
isSubmitting: boolean
/**
* A boolean indicating if any of the form fields have been touched.
*/
isTouched: boolean
/**
* A boolean indicating if any of the form's fields' values have been modified by the user. `True` if the user have modified at least one of the fields. Opposite of `isPristine`.
*/
isDirty: boolean
/**
* A boolean indicating if none of the form's fields' values have been modified by the user. `True` if the user have not modified any of the fields. Opposite of `isDirty`.
*/
isPristine: boolean
/**
* A boolean indicating if the form has been submitted.
*/
isSubmitted: boolean
/**
* A boolean indicating if the form or any of its fields are currently validating.
*/
isValidating: boolean
/**
* A boolean indicating if the form and all its fields are valid.
*/
isValid: boolean
/**
* A boolean indicating if the form can be submitted based on its current state.
*/
canSubmit: boolean
/**
* A counter for tracking the number of submission attempts.
*/
submissionAttempts: number
}
function getDefaultFormState<TFormData>(
defaultState: Partial<FormState<TFormData>>,
): FormState<TFormData> {
return {
values: defaultState.values ?? ({} as never),
errors: defaultState.errors ?? [],
errorMap: defaultState.errorMap ?? {},
fieldMeta: defaultState.fieldMeta ?? ({} as never),
canSubmit: defaultState.canSubmit ?? true,
isFieldsValid: defaultState.isFieldsValid ?? false,
isFieldsValidating: defaultState.isFieldsValidating ?? false,
isFormValid: defaultState.isFormValid ?? false,
isFormValidating: defaultState.isFormValidating ?? false,
isSubmitted: defaultState.isSubmitted ?? false,
isSubmitting: defaultState.isSubmitting ?? false,
isTouched: defaultState.isTouched ?? false,
isPristine: defaultState.isPristine ?? true,
isDirty: defaultState.isDirty ?? false,
isValid: defaultState.isValid ?? false,
isValidating: defaultState.isValidating ?? false,
submissionAttempts: defaultState.submissionAttempts ?? 0,
validationMetaMap: defaultState.validationMetaMap ?? {
onChange: undefined,
onBlur: undefined,
onSubmit: undefined,
onMount: undefined,
onServer: undefined,
},
}
}
/**
* A class representing the Form API. It handles the logic and interactions with the form state.
*
* Normally, you will not need to create a new `FormApi` instance directly. Instead, you will use a framework
* hook/function like `useForm` or `createForm` to create a new instance for you that uses your framework's reactivity model.
* However, if you need to create a new instance manually, you can do so by calling the `new FormApi` constructor.
*/
export class FormApi<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
> {
/**
* The options for the form.
*/
options: FormOptions<TFormData, TFormValidator> = {}
/**
* A [TanStack Store instance](https://tanstack.com/store/latest/docs/reference/Store) that keeps track of the form's state.
*/
store!: Store<FormState<TFormData>>
/**
* The current state of the form.
*
* **Note:**
* Do not use `state` directly, as it is not reactive.
* Please use form.useStore() utility to subscribe to state
*/
state!: FormState<TFormData>
/**
* A record of field information for each field in the form.
*/
fieldInfo: Record<DeepKeys<TFormData>, FieldInfo<TFormData, TFormValidator>> =
{} as any
/**
* @private
*/
prevTransformArray: unknown[] = []
/**
* Constructs a new `FormApi` instance with the given form options.
*/
constructor(opts?: FormOptions<TFormData, TFormValidator>) {
this.store = new Store<FormState<TFormData>>(
getDefaultFormState({
...(opts?.defaultState as any),
values: opts?.defaultValues ?? opts?.defaultState?.values,
isFormValid: true,
}),
{
onUpdate: () => {
let { state } = this.store
// Computed state
const fieldMetaValues = Object.values(state.fieldMeta) as (
| FieldMeta
| undefined
)[]
const isFieldsValidating = fieldMetaValues.some(
(field) => field?.isValidating,
)
const isFieldsValid = !fieldMetaValues.some(
(field) =>
field?.errorMap &&
isNonEmptyArray(Object.values(field.errorMap).filter(Boolean)),
)
const isTouched = fieldMetaValues.some((field) => field?.isTouched)
const isDirty = fieldMetaValues.some((field) => field?.isDirty)
const isPristine = !isDirty
const isValidating = isFieldsValidating || state.isFormValidating
state.errors = Object.values(state.errorMap).filter(
(val: unknown) => val !== undefined,
)
const isFormValid = state.errors.length === 0
const isValid = isFieldsValid && isFormValid
const canSubmit =
(state.submissionAttempts === 0 && !isTouched) ||
(!isValidating && !state.isSubmitting && isValid)
state = {
...state,
isFieldsValidating,
isFieldsValid,
isFormValid,
isValid,
canSubmit,
isTouched,
isPristine,
isDirty,
}
this.state = state
this.store.state = this.state
// Only run transform if state has shallowly changed - IE how React.useEffect works
const transformArray = this.options.transform?.deps ?? []
const shouldTransform =
transformArray.length !== this.prevTransformArray.length ||
transformArray.some((val, i) => val !== this.prevTransformArray[i])
if (shouldTransform) {
// This mutates the state
this.options.transform?.fn(this)
this.store.state = this.state
this.prevTransformArray = transformArray
}
},
},
)
this.state = this.store.state
this.update(opts || {})
}
/**
* @private
*/
runValidator<
TValue extends { value: TFormData; formApi: FormApi<any, any> },
TType extends 'validate' | 'validateAsync',
>(props: {
validate: TType extends 'validate'
? FormValidateOrFn<TFormData, TFormValidator>
: FormAsyncValidateOrFn<TFormData, TFormValidator>
value: TValue
type: TType
}): ReturnType<ReturnType<Validator<any>>[TType]> {
const adapter = this.options.validatorAdapter
if (adapter && typeof props.validate !== 'function') {
return adapter()[props.type](props.value, props.validate) as never
}
return (props.validate as FormValidateFn<any, any>)(props.value) as never
}
mount = () => {
const { onMount } = this.options.validators || {}
if (!onMount) return
const error = this.runValidator({
validate: onMount,
value: {
value: this.state.values,
formApi: this,
},
type: 'validate',
})
if (error) {
this.store.setState((prev) => ({
...prev,
errorMap: { ...prev.errorMap, onMount: error },
}))
}
}
/**
* Updates the form options and form state.
*/
update = (options?: FormOptions<TFormData, TFormValidator>) => {
if (!options) return
const oldOptions = this.options
// Options need to be updated first so that when the store is updated, the state is correct for the derived state
this.options = options
this.store.batch(() => {
const shouldUpdateValues =
options.defaultValues &&
options.defaultValues !== oldOptions.defaultValues &&
!this.state.isTouched
const shouldUpdateState =
options.defaultState !== oldOptions.defaultState &&
!this.state.isTouched
this.store.setState(() =>
getDefaultFormState(
Object.assign(
{},
this.state as any,
shouldUpdateState ? options.defaultState : {},
shouldUpdateValues
? {
values: options.defaultValues,
}
: {},
),
),
)
})
}
/**
* Resets the form state to the default values.
*/
reset = () => {
const { fieldMeta: currentFieldMeta } = this.state
const fieldMeta = this.resetFieldMeta(currentFieldMeta)
this.store.setState(() =>
getDefaultFormState({
...(this.options.defaultState as any),
values: this.options.defaultValues ?? this.options.defaultState?.values,
fieldMeta,
}),
)
}
applyFieldsTransformation = () => {
this.store.batch(() => {
void (
Object.values(this.fieldInfo) as FieldInfo<any, TFormValidator>[]
).forEach((field) => {
if (!field.instance) return
const fieldInstance = field.instance
// Apply transformation
const transformOnSubmit = fieldInstance.options.transformOnSubmit
if (!transformOnSubmit) return
const transformedValue = transformOnSubmit(field.instance.getValue())
if (!transformedValue) return
fieldInstance.setValue(transformedValue)
})
})
}
/**
* Validates all fields in the form using the correct handlers for a given validation type.
*/
validateAllFields = async (cause: ValidationCause) => {
const fieldValidationPromises: Promise<ValidationError[]>[] = [] as any
this.store.batch(() => {
void (
Object.values(this.fieldInfo) as FieldInfo<any, TFormValidator>[]
).forEach((field) => {
if (!field.instance) return
const fieldInstance = field.instance
// Validate the field
fieldValidationPromises.push(
Promise.resolve().then(() => fieldInstance.validate(cause)),
)
// If any fields are not touched
if (!field.instance.state.meta.isTouched) {
// Mark them as touched
field.instance.setMeta((prev) => ({ ...prev, isTouched: true }))
}
})
})
const fieldErrorMapMap = await Promise.all(fieldValidationPromises)
return fieldErrorMapMap.flat()
}
/**
* 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.
*/
validateArrayFieldsStartingFrom = async <TField extends DeepKeys<TFormData>>(
field: TField,
index: number,
cause: ValidationCause,
) => {
const currentValue = this.getFieldValue(field)
const lastIndex = Array.isArray(currentValue)
? Math.max(currentValue.length - 1, 0)
: null
// We have to validate all fields that have shifted (at least the current field)
const fieldKeysToValidate = [`${field}[${index}]`]
for (let i = index + 1; i <= (lastIndex ?? 0); i++) {
fieldKeysToValidate.push(`${field}[${i}]`)
}
// We also have to include all fields that are nested in the shifted fields
const fieldsToValidate = Object.keys(this.fieldInfo).filter((fieldKey) =>
fieldKeysToValidate.some((key) => fieldKey.startsWith(key)),
) as DeepKeys<TFormData>[]
// Validate the fields
const fieldValidationPromises: Promise<ValidationError[]>[] = [] as any
this.store.batch(() => {
fieldsToValidate.forEach((nestedField) => {
fieldValidationPromises.push(
Promise.resolve().then(() => this.validateField(nestedField, cause)),
)
})
})
const fieldErrorMapMap = await Promise.all(fieldValidationPromises)
return fieldErrorMapMap.flat()
}
/**
* Validates a specified field in the form using the correct handlers for a given validation type.
*/
validateField = <TField extends DeepKeys<TFormData>>(
field: TField,
cause: ValidationCause,
) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const fieldInstance = this.fieldInfo[field]?.instance
if (!fieldInstance) return []
// If the field is not touched (same logic as in validateAllFields)
if (!fieldInstance.state.meta.isTouched) {
// Mark it as touched
fieldInstance.setMeta((prev) => ({ ...prev, isTouched: true }))
}
return fieldInstance.validate(cause)
}
/**
* TODO: This code is copied from FieldApi, we should refactor to share
* @private
*/
validateSync = (cause: ValidationCause) => {
const validates = getSyncValidatorArray(cause, this.options)
let hasErrored = false as boolean
this.store.batch(() => {
for (const validateObj of validates) {
if (!validateObj.validate) continue
const error = normalizeError(
this.runValidator({
validate: validateObj.validate,
value: {
value: this.state.values,
formApi: this,
},
type: 'validate',
}),
)
const errorMapKey = getErrorMapKey(validateObj.cause)
if (this.state.errorMap[errorMapKey] !== error) {
this.store.setState((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
[errorMapKey]: error,
},
}))
}
if (error) {
hasErrored = true
}
}
})
/**
* when we have an error for onSubmit in the state, we want
* to clear the error as soon as the user enters a valid value in the field
*/
const submitErrKey = getErrorMapKey('submit')
if (
this.state.errorMap[submitErrKey] &&
cause !== 'submit' &&
!hasErrored
) {
this.store.setState((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
[submitErrKey]: undefined,
},
}))
}
return { hasErrored }
}
/**
* @private
*/
validateAsync = async (
cause: ValidationCause,
): Promise<ValidationError[]> => {
const validates = getAsyncValidatorArray(cause, this.options)
if (!this.state.isFormValidating) {
this.store.setState((prev) => ({ ...prev, isFormValidating: true }))
}
/**
* We have to use a for loop and generate our promises this way, otherwise it won't be sync
* when there are no validators needed to be run
*/
const promises: Promise<ValidationError | undefined>[] = []
for (const validateObj of validates) {
if (!validateObj.validate) continue
const key = getErrorMapKey(validateObj.cause)
const fieldValidatorMeta = this.state.validationMetaMap[key]
fieldValidatorMeta?.lastAbortController.abort()
const controller = new AbortController()
this.state.validationMetaMap[key] = {
lastAbortController: controller,
}
promises.push(
new Promise<ValidationError | undefined>(async (resolve) => {
let rawError!: ValidationError | undefined
try {
rawError = await new Promise((rawResolve, rawReject) => {
setTimeout(async () => {
if (controller.signal.aborted) return rawResolve(undefined)
try {
rawResolve(
await this.runValidator({
validate: validateObj.validate!,
value: {
value: this.state.values,
formApi: this,
signal: controller.signal,
},
type: 'validateAsync',
}),
)
} catch (e) {
rawReject(e)
}
}, validateObj.debounceMs)
})
} catch (e: unknown) {
rawError = e as ValidationError
}
const error = normalizeError(rawError)
this.store.setState((prev) => ({
...prev,
errorMap: {
...prev.errorMap,
[getErrorMapKey(cause)]: error,
},
}))
resolve(error)
}),
)
}
let results: ValidationError[] = []
if (promises.length) {
results = await Promise.all(promises)
}
this.store.setState((prev) => ({
...prev,
isFormValidating: false,
}))
return results.filter(Boolean)
}
/**
* @private
*/
validate = (
cause: ValidationCause,
): ValidationError[] | Promise<ValidationError[]> => {
// Attempt to sync validate first
const { hasErrored } = this.validateSync(cause)
if (hasErrored && !this.options.asyncAlways) {
return this.state.errors
}
// No error? Attempt async validation
return this.validateAsync(cause)
}
/**
* Handles the form submission, performs validation, and calls the appropriate onSubmit or onInvalidSubmit callbacks.
*/
handleSubmit = async () => {
this.store.setState((old) => ({
...old,
// Submission attempts mark the form as not submitted
isSubmitted: false,
// Count submission attempts
submissionAttempts: old.submissionAttempts + 1,
}))
// Don't let invalid forms submit
if (!this.state.canSubmit) return
this.store.setState((d) => ({ ...d, isSubmitting: true }))
const done = () => {
this.store.setState((prev) => ({ ...prev, isSubmitting: false }))
}
// Validate all fields
await this.validateAllFields('submit')
// Fields are invalid, do not submit
if (!this.state.isFieldsValid) {
done()
this.options.onSubmitInvalid?.({
value: this.state.values,
formApi: this,
})
return
}
// Run validation for the form
await this.validate('submit')
if (!this.state.isValid) {
done()
this.options.onSubmitInvalid?.({
value: this.state.values,
formApi: this,
})
return
}
try {
// Apply transformOnSubmit function
this.applyFieldsTransformation()
// Run the submit code
await this.options.onSubmit?.({ value: this.state.values, formApi: this })
this.store.batch(() => {
this.store.setState((prev) => ({ ...prev, isSubmitted: true }))
done()
})
} catch (err) {
done()
throw err
}
}
/**
* Gets the value of the specified field.
*/
getFieldValue = <TField extends DeepKeys<TFormData>>(
field: TField,
): DeepValue<TFormData, TField> => getBy(this.state.values, field)
/**
* Gets the metadata of the specified field.
*/
getFieldMeta = <TField extends DeepKeys<TFormData>>(
field: TField,
): FieldMeta | undefined => {
return this.state.fieldMeta[field]
}
/**
* Gets the field info of the specified field.
*/
getFieldInfo = <TField extends DeepKeys<TFormData>>(
field: TField,
): FieldInfo<TFormData, TFormValidator> => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return (this.fieldInfo[field] ||= {
instance: null,
validationMetaMap: {
onChange: undefined,
onBlur: undefined,
onSubmit: undefined,
onMount: undefined,
onServer: undefined,
},
})
}
/**
* Updates the metadata of the specified field.
*/
setFieldMeta = <TField extends DeepKeys<TFormData>>(
field: TField,
updater: Updater<FieldMeta>,
) => {
this.store.setState((prev) => {
return {
...prev,
fieldMeta: {
...prev.fieldMeta,
[field]: functionalUpdate(updater, prev.fieldMeta[field]),
},
}
})
}
resetFieldMeta = <TField extends DeepKeys<TFormData>>(
fieldMeta: Record<TField, FieldMeta>,
): Record<TField, FieldMeta> => {
return Object.keys(fieldMeta).reduce(
(acc: Record<TField, FieldMeta>, key) => {
const fieldKey = key as TField
acc[fieldKey] = {
isValidating: false,
isTouched: false,
isDirty: false,
isPristine: true,
errors: [],
errorMap: {},
}
return acc
},
{} as Record<TField, FieldMeta>,
)
}
/**
* Sets the value of the specified field and optionally updates the touched state.
*/
setFieldValue = <TField extends DeepKeys<TFormData>>(
field: TField,
updater: Updater<DeepValue<TFormData, TField>>,
opts?: UpdateMetaOptions,
) => {
const dontUpdateMeta = opts?.dontUpdateMeta ?? false
this.store.batch(() => {
if (!dontUpdateMeta) {
this.setFieldMeta(field, (prev) => ({
...prev,
isTouched: true,
isDirty: true,
}))
}
this.store.setState((prev) => {
return {
...prev,
values: setBy(prev.values, field, updater),
}
})
})
}
deleteField = <TField extends DeepKeys<TFormData>>(field: TField) => {
this.store.setState((prev) => {
const newState = { ...prev }
newState.values = deleteBy(newState.values, field)
delete newState.fieldMeta[field]
return newState
})
delete this.fieldInfo[field]
}
/**
* Pushes a value into an array field.
*/
pushFieldValue = <TField extends DeepKeys<TFormData>>(
field: TField,
value: DeepValue<TFormData, TField> extends any[]
? DeepValue<TFormData, TField>[number]
: never,
opts?: UpdateMetaOptions,
) => {
this.setFieldValue(
field,
(prev) => [...(Array.isArray(prev) ? prev : []), value] as any,
opts,
)
this.validateField(field, 'change')
}
/**
* Inserts a value into an array field at the specified index, shifting the subsequent values to the right.
*/
insertFieldValue = async <TField extends DeepKeys<TFormData>>(
field: TField,
index: number,
value: DeepValue<TFormData, TField> extends any[]
? DeepValue<TFormData, TField>[number]
: never,
opts?: UpdateMetaOptions,
) => {
this.setFieldValue(
field,
(prev) => {
return [
...(prev as DeepValue<TFormData, TField>[]).slice(0, index),
value,
...(prev as DeepValue<TFormData, TField>[]).slice(index),
] as any
},
opts,
)