When working with put/update method the button is disabled until each and every field is touched and atleast on value in each field is changed. whereas post method is working perfectly fine.
Employee.component.html
<form
fxLayout="column"
[formGroup]="editForm"
#f="ngForm" (ngSubmit)="onSubmit()"
>
<div class="input-row">
<mat-form-field fxFlex>
<mat-label>Id</mat-label>
<input readonly
value="{{data.emp.id}}"
matInput #id disabled
formControlName="id"
required
/>
</mat-form-field>
</div>
<div class="input-row">
<mat-form-field fxFlex>
<mat-label>Name</mat-label>
<input
value="{{data.emp.name}}"
matInput
placeholder="name"
formControlName="name"
required
/>
</mat-form-field>
</div>
<div class="input-row">
<mat-form-field fxFlex>
<mat-label>Designation</mat-label>
<input
value="{{data.emp.designation}}"
matInput
placeholder="designation"
formControlName="designation"
required
/>
</mat-form-field>
</div>
<br />
<div class="">
<button
mat-raised-button
type="reset"
class="btn btn-danger width"
(click)="close()">
Close</button
>
<button
mat-raised-button
[disabled]="!f.valid"
right
class="btn btn-success width right"
type="submit">
Update
</button>
</div>
</form>
Employee.component.ts
editForm: FormGroup;
constructor(
public dialogRef: MatDialogRef<EmployeeComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private service: EmployeeService
) {}
ngOnInit() {
this.editForm = new FormGroup({
id: new FormControl({ disabled: true }, Validators.required),
name: new FormControl("", [
Validators.required,
Validators.pattern(
/^[A-Za-z]{1,16}([ ]?[a-zA-Z]{0,16})([ ]?[a-zA-Z]{0,16})$/
)
]),
designation: new FormControl("", [
Validators.required,
Validators.pattern(
/^[A-Za-z]{1,16}([ ]?[a-zA-Z]{0,16})([ ]?[a-zA-Z]{0,16})$/
)
])
});
}
onSubmit() {
console.log(this.editForm.value);
this.service.updateEmployee(this.editForm.value).subscribe(
data => {
this.dialogRef.close();
},
err => {
console.log(err);
}
);
}
close() {
this.dialogRef.close();
}
Even when working with template driven form, I'm facing the same problem.