Skip to content

Commit 123b569

Browse files
authored
signalforms: return false instead of preventDefault() (#44)
1 parent 074bf86 commit 123b569

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

blog/2025-10-signal-forms-part1/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Angular Signal Forms Part 1: Getting Started with the Basics"
33
author: Danny Koppenhagen and Ferdinand Malcher
44
mail: dannyferdigravatar@fmalcher.de # Gravatar
55
published: 2025-10-13
6-
lastModified: 2025-10-22
6+
lastModified: 2025-11-09
77
keywords:
88
- Angular
99
- Signals
@@ -164,7 +164,7 @@ Notice, that we also use the form attribute `novalidate`: It disables the native
164164
We will handle validation later by using a form schema.
165165

166166
```html
167-
<form (submit)="submitForm($event)" novalidate>
167+
<form (submit)="submitForm()" novalidate>
168168
<div>
169169
<label for="username">Username</label>
170170
<input id="username" type="text" [field]="registrationForm.username" />
@@ -258,7 +258,8 @@ Signal Forms provide two approaches for handling form submission:
258258
Basic synchronous submission and the more powerful `submit()` function for asynchronous operations.
259259

260260
All approaches start with a form submission event handler: In the template, we already used the `(submit)` event binding on the `<form>` element.
261-
It is always necessary to prevent the default form submission behavior by calling `e.preventDefault()` in our `submitForm()` handler method.
261+
It is necessary to prevent the default form submission behavior by synchronously returning `false` from our `submitForm()` handler method.
262+
Otherwise, the page will reload on form submission.
262263

263264
### Basic Synchronous Submission
264265

@@ -268,12 +269,13 @@ For basic cases where you want to process form data synchronously, you can direc
268269
// ...
269270
export class RegistrationForm {
270271
// ...
271-
protected submitForm(e: Event) {
272-
e.preventDefault();
273-
272+
protected submitForm() {
274273
// Access current form data
275274
const formData = this.registrationModel();
276275
console.log('Form submitted:', formData);
276+
277+
// Prevent reloading (default browser behavior)
278+
return false;
277279
}
278280
}
279281
```
@@ -316,14 +318,14 @@ export class RegistrationForm {
316318
// ...
317319
readonly #registrationService = inject(RegistrationService);
318320
// ...
319-
protected async submitForm(e: Event) {
320-
e.preventDefault();
321-
322-
await submit(this.registrationForm, async (form) => {
321+
protected submitForm() {
322+
submit(this.registrationForm, async (form) => {
323323
await this.#registrationService.registerUser(form().value());
324324
console.log('Registration successful!');
325325
this.resetForm();
326326
});
327+
328+
return false;
327329
}
328330

329331
protected resetForm() {
@@ -340,7 +342,7 @@ When submitting the form we can now see that the value of the `submitting()` sig
340342
After the asynchronous operation is complete, it switches back to `false`.
341343

342344
```html
343-
<form (submit)="submitForm($event)">
345+
<form (submit)="submitForm()">
344346
<!-- ... -->
345347

346348
<button

blog/2025-10-signal-forms-part2/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Angular Signal Forms Part 2: Advanced Validation and Schema Patterns'
33
author: Danny Koppenhagen and Ferdinand Malcher
44
mail: dannyferdigravatar@fmalcher.de # Gravatar
55
published: 2025-10-15
6-
lastModified: 2025-10-30
6+
lastModified: 2025-11-09
77
keywords:
88
- Angular
99
- Signals
@@ -451,17 +451,15 @@ While client-side validation catches most errors before submission, server-side
451451
Signal Forms provide an elegant way to handle these errors and display them to users with proper field-level feedback.
452452
When using the `submit()` function, we can return an array of validation errors from the submission callback to assign them to specific fields or to the form itself.
453453

454-
The helper type `WithField` ensures that each error contains a reference to the field it belongs to.
454+
The type `ValidationErrorWithField` ensures that each error contains a reference to the field it belongs to.
455455

456456
```typescript
457-
import { /* ... */, WithField, ValidationErrorWithField } from '@angular/forms/signals';
457+
import { /* ... */, ValidationErrorWithField } from '@angular/forms/signals';
458458

459459
export class RegistrationForm {
460460
// ...
461-
protected async submitForm(e: Event) {
462-
e.preventDefault();
463-
464-
await submit(this.registrationForm, async (form) => {
461+
protected submitForm() {
462+
submit(this.registrationForm, async (form) => {
465463
const errors: ValidationErrorWithField[] = [];
466464

467465
try {
@@ -490,6 +488,8 @@ export class RegistrationForm {
490488

491489
return errors;
492490
});
491+
492+
return false;
493493
}
494494
}
495495
```

blog/2025-10-signal-forms-part3/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Angular Signal Forms Part 3: Child Forms and Custom UI Controls'
33
author: Danny Koppenhagen and Ferdinand Malcher
44
mail: dannyferdigravatar@fmalcher.de # Gravatar
55
published: 2025-10-20
6-
lastModified: 2025-10-20
6+
lastModified: 2025-11-09
77
keywords:
88
- Angular
99
- Signals
@@ -205,7 +205,7 @@ Finally, we integrate the `IdentityForm` component in our main form template.
205205
To make things work, we pass the `identity` field tree of our main form to the child component via property binding.
206206

207207
```html
208-
<form (submit)="submit($event)">
208+
<form (submit)="submit()">
209209
<!-- ... -->
210210
<app-identity-form [identity]="registrationForm.identity" />
211211
<!-- ... -->

0 commit comments

Comments
 (0)