Validators
Data validation can be enforced using Validators.
Some Angular default validators are available:
Other predefined validators are also available:
isSameOrBeforeToday
: validates that a datepicker value is in the pasisSameOrAfterToday
: validates that a datepicker value is in the future
Form validation is triggered by default when the button set to validate the Form Group is pressed.
It's also possible to build custom validators inside the container application and reference them here by name.
Predefined validators
1. required
A required validator checks if the value exists.
Important to be used with other validators like a minlength one to check if there is no value at all.
2. minlength
Checks if the value has a minimum number of characters (no character at all will not trigger this validator so better use it together with a required validator).
3. maxlength
Checks if the value has a maximum number of characters (no character at all will not trigger this validator so better used with a required one).
4. min
Checks if a numeric value is smaller (no character will trigger this validator so better use it with a required one).
5. max
Checks if a numeric value is smaller (no character will trigger this validator so better use it with a required one).
6. email
Checks if you entered an email (no character will not trigger this validator so better used with a required one).
7. pattern
Checks if you entered a string that respects a pattern (in this case, a regex).
8. isSameOrBeforeToday
It can be added to datepicker fields.
Checks a date is today or in the past (no character will trigger this validator so better use it with a required one).
9. isSameOrAfterToday
Checks a date is today or in the past (no character will trigger this validator so better use it with a required one).
Custom validators
Custom validators can be created by a developer in the web application before you can use them.
Available configurations are:
- Validator name - name provided by the developer to uniquely identify the validator
- Type - sync/ async validator (for more details check this)
- Error Message - The message that will be displayed if the field is not valid
- Validator parameters - if the validator needs inputs to decide if the field is valid or not, you can pass them using this list
NOTE: the error that the validator returns MUST match the validator name.
For more details about custom validators please check this link.