Summary of Angular Forms Tutorial - 23 - Custom Validation
Built-in validators may not always match the exact use case of an application, so custom validators can be created.
In the video, a custom validator for the username field is created to display an error message if the username is "admin" to filter out spam registrations.
Custom validators are functions that can be written in the component file or in a separate file for reuse.
The custom validator function returns an object with an error message if validation fails, or null if it passes.
To use the custom validator, it needs to be added to the component file and specified in the validations array.
Error messages can be displayed in the HTML template based on the custom validation result.
Parameters can be passed to custom validators by creating a factory function that returns the validator function.
The factory function accepts a parameter (e.g., a string pattern) and returns the validator function to validate against that pattern.
By using parameters in custom validators, specific strings (e.g., "password") can be forbidden in form fields.
Speakers/sources
Notable Quotes
— 00:39 — « lets assume that the spam word is admin for our example. »
— 00:46 — « custom validator is nothing but a function. »
— 02:51 — « if the username contains the string admin we set the forbidden flag to true. »
— 05:44 — « custom validation works perfectly fine. »
— 06:01 — « we should be able to pass in the string we want to forbid as a parameter to our custom validator. »
Category
Educational