I have some input fields with different validation rules set on them. But I also want to add some validation to all the fields at once, when submitting, or make submit button disabled, if there are any errors above.
However, I haven't put all my inputs inside form tag from the beginning and have kind of trouble with that right now. I am new to all of this, so can you please help me? Is there any way to solve it, without recreating all the form? :(
I've tried adding:
<form #stepOneForm="ngForm">
<button type="submit [disabled]="!stepOneForm.form.valid" mat-button matStepperNext>Go to next step</button>
But it didnt help...
My code looks as below:
HTML
<!-- Name -->
<mat-form-field class="dcp-input-field">
<input matInput placeholder="Name" [formControl]="name" [errorStateMatcher]="matcher">
<mat-hint>Please enter your name</mat-hint>
<mat-error *ngIf="name.hasError('required')">
This is <strong>required</strong> field
</mat-error>
</mat-form-field>
<!-- DoB -->
<mat-form-field class="dcp-input-field">
<input matInput [matDatepicker]="dp" placeholder="Date of Birth" [formControl]="dob" [errorStateMatcher]="matcher">
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
<mat-datepicker #dp></mat-datepicker>
<mat-hint>Please enter your date of birth</mat-hint>
<mat-error *ngIf="dob.hasError('required')">
This is <strong>required</strong> field
</mat-error>
</mat-form-field>
<!-- PolicyNo -->
<mat-form-field class="dcp-input-field">
<input matInput placeholder="Policy number" [formControl]="policyNo" [errorStateMatcher]="matcher">
<mat-hint>Please enter your Policy number</mat-hint>
<mat-error *ngIf="policyNo.hasError('required')">
This is <strong>required</strong> field
</mat-error>
<mat-error *ngIf="policyNo.hasError('minlength')">
The value is <strong>too short</strong>
</mat-error>
</mat-form-field>
TS
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(
control: FormControl | null,
form: FormGroupDirective | NgForm | null
): boolean {
const isSubmitted = form && form.submitted;
return !!(
control &&
control.invalid &&
(control.dirty || control.touched || isSubmitted)
);
}
}
name = new FormControl("", [Validators.required]);
dob = new FormControl("", [Validators.required]);
policyNo = new FormControl("", [
Validators.required,
Validators.minLength(6)
]);
matcher = new MyErrorStateMatcher();
Thank you and sorry for noob question! ;)