add instant validation for rating

This commit is contained in:
Constantin Simonis 2024-12-17 09:19:02 +01:00
parent 99ae5fd016
commit 5706dfaae8
Signed by: csimonis
GPG Key ID: 758DD9C506603183

View File

@ -43,7 +43,7 @@ import {ErrorMsgComponent} from "./error-msg.component";
<br> <br>
<label for="rating">Rating</label> <label for="rating">Rating</label>
<br> <br>
<input id="rating" type="number" formControlName="rating" min="0" max="5"> <input id="rating" type="number" formControlName="rating">
<app-error-msg [msg]="errorMsgs['rating']"></app-error-msg> <app-error-msg [msg]="errorMsgs['rating']"></app-error-msg>
<br> <br>
<br> <br>
@ -103,6 +103,18 @@ export class EditHotelComponent implements OnInit {
control?.valueChanges?.pipe(debounceTime(1000)).subscribe(() => {this.setErrorMessage(controlName, control)}); control?.valueChanges?.pipe(debounceTime(1000)).subscribe(() => {this.setErrorMessage(controlName, control)});
}); });
this.form.controls['rating'].valueChanges.subscribe((value: number | null) => {
if (value === null) {
this.errorMsgs['rating'] = this.validationErrors['required'];
} else if (value < 0) {
this.errorMsgs['rating'] = this.validationErrors['min'];
} else if (value > 5) {
this.errorMsgs['rating'] = this.validationErrors['max'];
} else {
this.errorMsgs['rating'] = '';
}
})
} }
submit() { submit() {