add better validation to forms

This commit is contained in:
Jan-Marlon Leibl 2024-12-03 09:23:50 +01:00
parent 6e5eadd3a2
commit a6829fc3f5

View file

@ -3,11 +3,11 @@ import { HotelFormComponent } from './hotel-form/hotel-form.component';
import { HotelDetailsComponent } from './hotel-details/hotel-details.component'; import { HotelDetailsComponent } from './hotel-details/hotel-details.component';
export const formGuard: CanDeactivateFn<HotelFormComponent | HotelDetailsComponent> = (component: HotelFormComponent | HotelDetailsComponent, currentRoute, currentState, nextState) => { export const formGuard: CanDeactivateFn<HotelFormComponent | HotelDetailsComponent> = (component: HotelFormComponent | HotelDetailsComponent, currentRoute, currentState, nextState) => {
console.log(component) const hasUnsavedChanges =
if (component instanceof HotelFormComponent && component.form?.dirty) { (component instanceof HotelFormComponent && component.form?.dirty) ||
return confirm('You have unsaved changes. Do you really want to leave?'); (component instanceof HotelDetailsComponent && component.hotelForm?.form?.dirty);
}
else if (component instanceof HotelDetailsComponent && component.hotelForm?.form?.dirty) { if (hasUnsavedChanges) {
return confirm('You have unsaved changes. Do you really want to leave?'); return confirm('You have unsaved changes. Do you really want to leave?');
} }