File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Form utility helpers
2+ export function serializeForm ( form : HTMLFormElement ) : Record < string , string > {
3+ const data : Record < string , string > = { } ;
4+ const formData = new FormData ( form ) ;
5+ formData . forEach ( ( value , key ) => { data [ key ] = value . toString ( ) ; } ) ;
6+ return data ;
7+ }
8+ export function resetForm ( form : HTMLFormElement ) : void { form . reset ( ) ; }
9+ export function isFormDirty ( form : HTMLFormElement , originalValues : Record < string , string > ) : boolean {
10+ const currentValues = serializeForm ( form ) ;
11+ return Object . keys ( originalValues ) . some ( key => originalValues [ key ] !== currentValues [ key ] ) ;
12+ }
13+ export function setFormErrors ( errors : Record < string , string > ) : void {
14+ Object . entries ( errors ) . forEach ( ( [ field , message ] ) => {
15+ const el = document . querySelector ( '[name=""' + field + '""]' ) ;
16+ if ( el ) el . setAttribute ( 'aria-invalid' , 'true' ) ;
17+ const errEl = document . getElementById ( field + '-error' ) ;
18+ if ( errEl ) errEl . textContent = message ;
19+ } ) ;
20+ }
You can’t perform that action at this time.
0 commit comments