I can't find out how to retrieve all fields modified by the user using angular2 forms. I did some research here and on angular2 official forms docs and I couldn't find such information.
This is how I do it using jQuery:
this.isFormDirty = function (form) {
var changeNames = [];
$(form).find(":input:not(:button):not([type=hidden])").each(function () {
if ((this.type == "text" || this.type == "textarea" || this.type == "number" || this.type == "hidden" || this.type == "file") && this.defaultValue != this.value) {
changeNames.push(this.name);
} else {
if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
changeNames.push(this.name);
} else {
if ((this.type == "select-one" || this.type == "select-multiple")) {
for (var x = 0; x < this.length; x++) {
if (this.options[x].selected != this.options[x].defaultSelected) {
changeNames.push(this.name);
}
}
}
}
}
});
return changeNames;
};
Is there a way to do that using angular2 forms? I thought I'd have some sort of changedValues property, but I can't find it.
EDIT
This is how my form is created: (permissionForm is of type FormGroup)
this.permissionForm = this.fb.group({
FullUrl: ['', Validators.required],
Area: ['', Validators.required],
Controller: ['', Validators.required],
Action: ['', Validators.required],
Name: ['', Validators.required],
Description: ['', Validators.required],
ParentPermissionId: ['', Validators.required],
ShowInMenu: ['', Validators.required],
Order: ['', Validators.required],
Icon: ['', Validators.required]
});
You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.
In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.
touched means the user has entered the form. dirty / ! pristine means the user has made a modification.
markAsDirty() is to mark all controls as dirty in the form. For this to work correctly, you'd have to also wrap it in a Clarity form container (which we don't have a generic wrapper yet, see #2864).
const keyValue = Object.entries(this.form.controls).filter(value => value[1].dirty);
For get control names array:
keyValue.map(value => value[0]);
For get controls array:
keyValue.map(value => value[1]);
For get controls object:
keyValue.reduce((a, v) => Object.assign(a, { [v[0]]: v[1] }), {});
Сompact function:
type GetDirtyControlsTypes = 'object' | 'array' | 'names';
export function getDirtyControls(form: FormGroup, type: GetDirtyControlsTypes = 'object') {
// keyValue
const kv = Object.entries(form.controls).filter(val => val[1].dirty);
return {
object: () => kv.reduce((accum, val) => Object.assign(accum, { [val[0]]: val[1] }), {}),
array: () => kv.map(val => val[1]),
names: () => kv.map(val => val[0]),
}[type]();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With