Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I only send edited/changed fields in angular 2+ reactive form when I edit?

I'm would like to send only edited/changed values from my form when I am editing.

At present, my form send all form values.

I am using interfaces :

export interface WorkExperience {
  id?: number;     
  name_company:string;     
  date_start:string;
  date_end:string;     
}
export interface WorkExperienceUpdate {
  id_work_experience: number;
  experience: WorkExperience;
}

I do this on my .ts :

experience: WorkExperience;

this.experience = this.navParams.get('experience');

this.registrationForm = formBuilder.group({         
        company: [this.experience.name_company, Validators.required],         
        date_start: [this.formatDate(this.experience.date_start), Validators.required],
        date_end: [this.experience.date_end ? this.formatDate(this.experience.date_end) : '']            
      });

My html is:

<form [formGroup]="registrationForm">              
          <ion-item>
            <ion-label floating>Company</ion-label>
            <ion-input type="text" formControlName="company"></ion-input>
          </ion-item>              
            <ion-label floating>Start Date</ion-label>
            <ion-datetime displayFormat="DD-MM-YYYY" formControlName="date_start"></ion-datetime>
          </ion-item>
          <ion-item>
            <ion-label floating>End Date</ion-label>
            <ion-datetime displayFormat="DD-MM-YYYY" formControlName="date_end"></ion-datetime>
          </ion-item>
        </form>

I've used the same form to add and edit, so when I am editing I only need send the elements that is not the same , like this :

  1. If edit only End Date , the object should be :

{ "id_educative_experience": 8, "educative_experience": { "date_end": "30-10-2017"
} }

and now with my way I am sending all values.

My submit is :

 submitRegistration(value): void {
    // EDIT
     let tempExp;
     tempExp = {          
              date_start: this.formatDate(value['date_start']),
              date_end: value['date_end'] ? this.formatDate(value['date_end']) : null,
              name_company: value['company'],         
            }

 let updateExperience: WorkExperienceUpdate = {
        experience: tempExp,
        id_work_experience: this.experience.id,
      };

 this._wp.updateWorkExperience(updateExperience).subscribe(r => {
        this.presentToast(`La experiencia laboral se ha modficado`);
        this.nav.pop().then(() => this.presentToast(`La experiencia laboral se ha modficado`));
      }, error => {
        this.nav.pop().then(() => {
          this.presentToast(`Ups, ha ocurrido un error.Inténtalo de nuevo`);
        });
      })
    }

How can i do that to achieve that send only edited values

Thank you in advance

like image 959
SDLUJO Avatar asked Feb 24 '26 06:02

SDLUJO


1 Answers

You can check for the dirty values of the form.

getDirtyValues(cg: FormGroup) {
  const dirtyValues = {};
  Object.keys(cg.controls).forEach(c => {
    const currentControl = cg.get(c);

    if (currentControl.dirty) {
      dirtyValues[c] = currentControl.value;
    }
  });
  return dirtyValues;
}
like image 50
deramko Avatar answered Feb 26 '26 22:02

deramko