Is there a way to make a field required in Angular only when a button is clicked? Currently, the error message appears even before the user interacts with the field.
I would like the error message "folder name is required" to only appear when the user clicks the submit button and the field is empty. Any suggestions? Thank you.
private _createModelForm(): FormGroup {
return this.formBuilder.group({
propertyId: this.data.propertyId,
folderName: new FormControl('', [
Validators.required,
Validators.pattern(/[a-zA-Z0-9\.\-]+.?/)
]),
documentManagementFolderId:this.data.documentManagementFolderId
});
}
<mat-error *ngIf="modelForm.get('folderName').hasError('pattern')">
Folder name must not include special characters.
</mat-error>