@@ -26,7 +26,7 @@ export function getInputModel(inputs: ReadonlyArray<FormInputModel>, fieldName:
2626
2727export function initializeValues < T > ( inputs : ReadonlyArray < FormInputModel > , formValues ?: T ) : void {
2828 inputs
29- . filter ( input => ! input . dirty )
29+ . filter ( input => ! input . dirtyOrTouched )
3030 . forEach ( input => {
3131 input . value = ! ! ( formValues as any ) ?. hasOwnProperty ( input . name )
3232 ? ( formValues as any ) [ input . name ]
@@ -37,7 +37,7 @@ export function initializeValues<T>(inputs: ReadonlyArray<FormInputModel>, formV
3737export function reset ( inputs : ReadonlyArray < FormInputModel > , formValue ?: any ) : void {
3838 inputs
3939 . forEach ( inputDef => {
40- inputDef . dirty = false
40+ inputDef . dirtyOrTouched = false
4141 inputDef . error = undefined
4242 inputDef . value = formValue ?. [ inputDef . name ]
4343 } )
@@ -56,7 +56,7 @@ export async function submitAsync<T, R>(
5656 setDisableButton ( true )
5757
5858 // if there are no dirty fields, display a message and stop submitting
59- const dirty : FormInputModel | undefined = inputs . find ( fieldDef => ! ! fieldDef . dirty )
59+ const dirty : FormInputModel | undefined = inputs . find ( fieldDef => ! ! fieldDef . dirtyOrTouched )
6060 if ( ! dirty ) {
6161 toast . info ( 'No changes detected.' )
6262 return
@@ -66,7 +66,7 @@ export async function submitAsync<T, R>(
6666 const formValues : HTMLFormControlsCollection = ( event . target as HTMLFormElement ) . elements
6767
6868 // if there are any validation errors, display a message and stop submitting
69- const isValid : boolean = validate ( inputs , formValues , true )
69+ const isValid : boolean = await validateAsync ( inputs , formValues , true )
7070 if ( ! isValid ) {
7171 toast . error ( 'Changes could not be saved. Please resolve errors.' )
7272 return Promise . reject ( ErrorMessage . submit )
@@ -85,30 +85,30 @@ export async function submitAsync<T, R>(
8585 } )
8686}
8787
88- export function validateAndUpdate ( event : FormEvent < HTMLFormElement > , inputs : ReadonlyArray < FormInputModel > ) : boolean {
88+ export async function validateAndUpdateAsync ( event : FormEvent < HTMLFormElement > , inputs : ReadonlyArray < FormInputModel > ) : Promise < boolean > {
8989
9090 const input : HTMLInputElement = ( event . target as HTMLInputElement )
9191 // set the input def info
9292 const inputDef : FormInputModel = getInputModel ( inputs , input . name )
93- inputDef . dirty = true
93+ inputDef . dirtyOrTouched = true
9494 inputDef . value = input . value
9595
9696 // validate the form
9797 const formElements : HTMLFormControlsCollection = ( input . form as HTMLFormElement ) . elements
98- const isValid : boolean = validate ( inputs , formElements )
98+ const isValid : boolean = await validateAsync ( inputs , formElements )
9999
100100 return isValid
101101}
102102
103- function validate ( inputs : ReadonlyArray < FormInputModel > , formElements : HTMLFormControlsCollection , formDirty ?: boolean ) : boolean {
103+ async function validateAsync ( inputs : ReadonlyArray < FormInputModel > , formElements : HTMLFormControlsCollection , formDirty ?: boolean ) : Promise < boolean > {
104104 const errors : ReadonlyArray < FormInputModel > = inputs
105105 . filter ( formInputDef => {
106106 formInputDef . error = undefined
107- formInputDef . dirty = formInputDef . dirty || ! ! formDirty
108- formInputDef . validators
109- . forEach ( validator => {
107+ formInputDef . dirtyOrTouched = formInputDef . dirtyOrTouched || ! ! formDirty
108+ formInputDef . validateOnChange
109+ ? .forEach ( async validator => {
110110 if ( ! formInputDef . error ) {
111- formInputDef . error = validator ( formInputDef . value , formElements , formInputDef . dependentField )
111+ formInputDef . error = await validator ( formInputDef . value , formElements , formInputDef . dependentField )
112112 }
113113 } )
114114 return ! ! formInputDef . error
0 commit comments