In my code base using an Angular form, the user opens an entry to be edited and the various fields are pre-filled with the current values. The Save button starts disabled, because nothing isdirty
in an Angular sense. If the user enters a field and starts typing, the field becomes dirty and the Save becomes enabled.
During the course of editing the form values, there are checks that disable the Save button if:
- required fields are empty,
- fields requiring a certain format have invalid entries,
- entries are longer than permitted,
... and the like, and finally
- the value, even if
dirty
, is now back to the original value when the user opened the form.
Here is the essence of it:
<!-- template code -->
<input formControlName="name" type="text" (keyup)="handleNameChange()">
. . .
<button [disabled]="!updateForm.valid || disableSave">Save</button>
. . .
// backing code
public handleNameChange(): void {
this.disableSave = this.updateForm.controls.name.value === this.original.name;
}
Two questions:
A. Though it seems "obvious" from one perspective (nothing has changed so why should one have to save it?), is the last point a common, best practice in the UX sense? Would love to see articles or references about it.
B. Is there a way for an Angular form to do this (i.e. set a field back to non-dirty) automatically without my having to add explicit code to check the new value against the old value?