examples/angular/to-resource-signal-forms
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Angular Query toResource Signal Forms Example#
A registration form that validates the username and invite codes with queries, through Signal Forms' validateAsync.
How it works#
src/app/validation-queries.service.tsis an@InjectableValidationQueriesServicewhoseusernameAvailability(username)andinviteCodeValidation(code)returnqueryOptions, and whosesubmitRegistration()returnsmutationOptions. All three use mock requests.src/app/app.component.tspasses afactorytovalidateAsyncthat creates a query withinjectQueryand returnstoResource(query). Results are cached by query key, and the query isenabledonly when the field has a value.- The invite-code validator is added with
applyEach, so its query is created only when an invite code is added to the form array. - The fields use
debounce(path, 300)from@angular/forms/signalsbecause thedebounceoption ofvalidateAsyncdoes not work withtoResource(query)(angular/angular#70884). Blurring a field or submitting the form applies a pending update immediately. - Submitting calls
registrationMutation.mutateAsyncfrominjectMutationinside Signal Forms'submit, and maps the errors the mutation returns to their fields. The "Request validation again" button callsreloadValidation().
Signal Forms' validateAsync is stable from Angular 22, so this example needs Angular 22.
To run this example:
npm installnpm start