add validation for submit

This commit is contained in:
Constantin Simonis 2024-12-17 08:55:43 +01:00
parent 2503c33005
commit 99ae5fd016
Signed by: csimonis
GPG Key ID: 758DD9C506603183

View File

@ -107,7 +107,8 @@ export class EditHotelComponent implements OnInit {
submit() { submit() {
if (!this.form.valid) { if (!this.form.valid) {
this.showErrors();
return;
} }
const hotel: Hotel = { const hotel: Hotel = {
@ -140,4 +141,15 @@ export class EditHotelComponent implements OnInit {
setErrorMessage(controlName: string, control: AbstractControl) { setErrorMessage(controlName: string, control: AbstractControl) {
this.errorMsgs[controlName] = Object.keys(control.errors ?? {}).map ((key) => this.validationErrors[key]).join(' '); this.errorMsgs[controlName] = Object.keys(control.errors ?? {}).map ((key) => this.validationErrors[key]).join(' ');
} }
private showErrors() {
Object.keys(this.form.controls).forEach(controlName => {
const control = this.form.get(controlName);
if (!control) {
return;
}
this.setErrorMessage(controlName, control);
});
}
} }