Skip to content

Instantly share code, notes, and snippets.

@jdspiral
Created August 28, 2019 23:47
Show Gist options
  • Save jdspiral/fef5edbccff6a27a46ddde7781293219 to your computer and use it in GitHub Desktop.
Save jdspiral/fef5edbccff6a27a46ddde7781293219 to your computer and use it in GitHub Desktop.
Check for dirty reactive form values
public getDirtyValues(form: any) {
let dirtyValues = {};
Object.keys(form.controls).forEach(key => {
const currentControl = form.controls[key];
if (currentControl.dirty) {
if (currentControl.controls) {
dirtyValues[key] = this.getDirtyValues(currentControl);
} else {
dirtyValues[key] = currentControl.value;
}
}
});
return dirtyValues;
}
const dirtyValues = this.getDirtyValues(this.userForm);
// Only update those values that have changed in the form
Object.keys(dirtyValues).forEach(key => {
if (key !== 'password' && key) {
data = {
id: this.selectedUser.id,
username: this.selectedUser.email,
password: this.selectedUser.password,
securityToken: this.selectedUser.securityToken,
status: 1,
...(key && { [key]: this.userForm.get(key).value })
};
} else {
data = {
id: this.selectedUser.id,
username: this.selectedUser.email,
password: this.selectedUser.password,
securityToken: this.selectedUser.securityToken,
status: 1,
...(key && {[key]: this.userForm.get(key).value})
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment