Skip to content

Commit 6c3aada

Browse files
fmalcherd-koppenhagen
authored andcommitted
feat: use return false instead of preventDefault()
1 parent ea445f2 commit 6c3aada

6 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/app/registration-form-1/registration-form-1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<app-back-button />
22
<h1>Version 1: Getting Started with the Basics</h1>
33

4-
<form (submit)="submitForm($event)" novalidate>
4+
<form (submit)="submitForm()" novalidate>
55
<label
66
>Username
77
<input

src/app/registration-form-1/registration-form-1.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ export class RegistrationForm1 {
6363
.value.update((items) => items.filter((_, index) => index !== removeIndex));
6464
}
6565

66-
protected async submitForm(e: Event) {
67-
e.preventDefault();
68-
69-
await submit(this.registrationForm, async (form) => {
66+
protected submitForm() {
67+
submit(this.registrationForm, async (form) => {
7068
await this.#registrationService.registerUser(form().value);
7169
console.log('Registration successful!');
7270
this.resetForm();
7371
});
72+
73+
// Prevent reloading (default browser behavior)
74+
return false;
7475
}
7576

7677
protected resetForm() {

src/app/registration-form-2/registration-form-2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h1>Version 2: Advanced Validation and Schema Patterns</h1>
66
error.
77
</p>
88

9-
<form (submit)="submitForm($event)" novalidate>
9+
<form (submit)="submitForm()" novalidate>
1010
<label
1111
>Username
1212
<input

src/app/registration-form-2/registration-form-2.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ export class RegistrationForm2 {
146146
.value.update((items) => items.filter((_, index) => index !== removeIndex));
147147
}
148148

149-
protected async submitForm(e: Event) {
150-
e.preventDefault();
151-
149+
protected submitForm() {
152150
// validate when submitting and assign possible errors for matching field for showing in the UI
153-
await submit(this.registrationForm, async (form) => {
151+
submit(this.registrationForm, async (form) => {
154152
const errors: ValidationErrorWithField[] = [];
155153

156154
try {
@@ -168,6 +166,9 @@ export class RegistrationForm2 {
168166
setTimeout(() => this.resetForm(), 3000);
169167
return errors;
170168
});
169+
170+
// Prevent reloading (default browser behavior)
171+
return false;
171172
}
172173

173174
// Reset form

src/app/registration-form-3/registration-form-3.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h1>Version 3: Child Forms and Custom UI Controls</h1>
66
error.
77
</p>
88

9-
<form (submit)="submitForm($event)" novalidate>
9+
<form (submit)="submitForm()" novalidate>
1010
<label
1111
>Username
1212
<input

src/app/registration-form-3/registration-form-3.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,9 @@ export class RegistrationForm3 {
153153
.value.update((items) => items.filter((_, index) => index !== removeIndex));
154154
}
155155

156-
protected async submitForm(e: Event) {
157-
e.preventDefault();
158-
156+
protected submitForm() {
159157
// validate when submitting and assign possible errors for matching field for showing in the UI
160-
await submit(this.registrationForm, async (form) => {
158+
submit(this.registrationForm, async (form) => {
161159
const errors: ValidationErrorWithField[] = [];
162160

163161
try {
@@ -175,6 +173,9 @@ export class RegistrationForm3 {
175173

176174
return errors;
177175
});
176+
177+
// Prevent reloading (default browser behavior)
178+
return false;
178179
}
179180

180181
// Reset form

0 commit comments

Comments
 (0)