Reputation: 1135
I need to edit and update user record. I am using formGroup to submit my form. I need to pass and append the "id" field along with formgroup while submiting the update form.
this.Form = this.formBuilder.group({
firstname: ['', Validators.required],
lastname: ['', Validators.required],
email: ['', Validators.required]
});
i tried:-
editMethod(userid:any) {
this.Form.patchValue({
id: userid,
})
}
Patch value is not working I need to append id along with formGroup so that i can update this form.
Upvotes: 0
Views: 138
Reputation: 149
PatchValue don't work because you do not have declarated the control "id" when you build the FormGroup.
the right code:
this.Form = this.formBuilder.group({
id: [''],
firstname: ['', Validators.required],
lastname: ['', Validators.required],
email: ['', Validators.required]
});
editMethod(userid:any) {
this.Form.patchValue({
id: userid,
});
}
Upvotes: 1
Reputation: 465
You can define Id by default and keep it without validation of required.
and patch the value later.
this.Form = this.formBuilder.group({
id: [''], // add this
firstname: ['', Validators.required],
lastname: ['', Validators.required],
email: ['', Validators.required]
});
Moreover you should initialize for with id If it already present like
this.Form = this.formBuilder.group({
id: [userid],
firstname: ['', Validators.required],
lastname: ['', Validators.required],
email: ['', Validators.required]
});
At second time you no need to patch Value as it already present there.
Upvotes: 1