Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all "dirty" (modified) fields using angular2 forms?

Tags:

angular

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]
    });
like image 606
eestein Avatar asked Dec 01 '16 15:12

eestein


People also ask

How do you make a form control dirty?

You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.

What is a clean and preferred way to reset all objects in angular form?

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.

What is dirty reactive form?

touched means the user has entered the form. dirty / ! pristine means the user has made a modification.

What is mark as dirty?

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).


1 Answers

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]();
}
like image 84
Nikolay Avatar answered Oct 01 '22 09:10

Nikolay