In your typescript file, follow below steps
- Define FormGroup
addForm:FormGroup;
- Inject the FormBuilder Service in the constructor
constructor(private _formBuilder: FormBuilder){}
- Build Form in the ngOnInit Lifecycle
this.addForm = this._formBuilder.group({
message: ['', [Validators.required]],
});
Now In your HTML file, use below code.
<form [formGroup]="addForm">
<mat-form-field [floatLabel]="'always'">
<mat-label>Message</mat-label>
<textarea matInput [rows]="3" [formControlName]="'message'"></textarea>
<mat-error *ngIf="addForm.get('message').hasError('required')">
Message is required.
</mat-error>
</mat-form-field>
<button mat-flat-button [color]="'primary'" (click)="save()" [disabled]="addForm.invalid">
<span>Save</span>
</button>
</form>
If you want to disable until form validity then you can use invalid property of FormGroup as like I did.
Let me know if you've any queries.
Happy Learning. ✌️