Sub topic 8
Submit the form
This chapter assumes you can already create a form with form(), bind inputs with [field], and read field state as signals. Here you connect those pieces to a submit handler that checks validity, calls a server, and reports a failure.
What submit() does
submit() is a function from @angular/forms/signals. You give it the form and an async action. It does two things for you:
- It marks the fields as touched, so any error display that waits for
touched()now shows its messages. - It runs your action only if the form is valid.
If the form is invalid, the action never runs. You don't need an if (form().invalid()) return; guard.
A complete example
The component below wires submit() to a form element. The handler calls event.preventDefault() so the browser doesn't reload the page.
import { Component, signal } from '@angular/core';
import { form, Field, required, submit } from '@angular/forms/signals';
@Component({
selector: 'app-profile',
imports: [Field],
template: `
<form (submit)="onSubmit($event)">
<label>Name <input [field]="profileForm.name" /></label>
@if (profileForm.name().touched() && profileForm.name().invalid()) {
@for (error of profileForm.name().errors(); track error.kind) {
<p class="error">{{ error.message }}</p>
}
}
<button type="submit">Save</button>
</form>
`,
})
export class ProfileComponent {
private readonly model = signal({ name: '' });
protected readonly profileForm = form(this.model, (path) => {
required(path.name, { message: 'Name is required' });
});
protected async onSubmit(event: Event) {
event.preventDefault();
await submit(this.profileForm, async (f) => {
const response = await fetch('/api/profile', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(f().value()),
});
if (!response.ok) {
return { kind: 'server', message: 'Saving failed. Try again.' };
}
return undefined;
});
}
}Press the button with an empty name. The action doesn't run, and because submit() marked the field as touched, "Name is required" appears. This is the main reason to use submit() instead of reading this.model() directly in the handler. A plain handler would skip both the validity check and the touched marking.
The action and server errors
The action receives the form as its argument. In the example it is named f, and f().value() returns the current model data as a plain object.
The action decides the outcome by what it returns:
- Return
undefinedwhen the save worked. - Return an error object with a
kindand amessagewhen it failed. The error is then reported on the form, using the same shape as the validator errors you already know.
Because the action is async, submit() returns a promise. Await it if you need to run code after the attempt finishes, such as navigating away.
Common mistakes
- Forgetting
preventDefault(). The browser then performs its own form submission and reloads the page. - Disabling the button until the form is valid. The login example earlier in this tutorial does this with
[disabled]="loginForm().invalid()". It works, but the user never sees why the button is dead. Letting them press it, sosubmit()can reveal the errors, is often kinder. - Throwing on server failures. A network error makes
fetchreject. Wrap the call intry/catchand return an error object, or the failure won't show up as a form error.
Exercise
A user clicks the submit button while the email field is empty and required. The submit action calls your API. Does the API call happen, and what do the touched states look like afterward?
Show answer
The API call does not happen, because submit() runs the action only when the form is valid. submit() still marks the fields as touched, so the "required" error becomes visible under the email field.