Created
August 28, 2019 23:47
-
-
Save jdspiral/fef5edbccff6a27a46ddde7781293219 to your computer and use it in GitHub Desktop.
Check for dirty reactive form values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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