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 :
{
"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
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;
}
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