All the Angular’s built-in validators are supported.
The
Validators.email
but usingValidators.pattern
:/^[^@\s!(){}<>]+@[\w-]+(\.[A-Za-z]+)+$/
{
...
"validators": [
{
"name": "...",
"value": "...",
"message": "..."
}
],
"asyncValidators": [
// Same with "validators"
...
]
}
Name of the validator.
For the validator which needs value to validate like min
, max
.
Custom validation message for Angular's built-in validators. The placeholders available are:
{{value}}
{{validatorValue}}
We can set the custom validation message for Angular's built-in validators via message
property.
validators: [
{
"name": "email",
"message": "The current email format: "{{value}}" is invalid."
},
]
Result: The current email format: "123.com" is invalid.
validators: [
{
"name": "max",
"value": 100,
"message": "The value must not exceeds {{validatorValue}}!"
}
]
Result: The value must not exceeds 100!
If message
is not provided, then the default message set in the validationMessages
will be used.
providers: [
provideNgDynamicJsonForm({
validationMessages: {
required: 'This field is required.',
min: 'The value must exceeds {{value}}!'
}
}),
],
To add validators to the control, simply provide the validators
.
{
"label": "Text",
"formControlName": "text",
"validators": [
{
"name": "required",
"message": "Please fill in this input!"
}
]
}
Some of the validators like min
, max
requires value to validate, provide the value
so that the validator can works correctly.
{
"label": "Text",
"formControlName": "text",
"type": "number",
"validators": [
{
"name": "min",
"message": "Minimum value is 10",
"value": 10
}
]
}
To use custom validators, add them to the customValidators
in provideNgDynamicJsonForm()
provider. Then, set the name in the validator config to match with the key in customValidators
provider.
import { ApplicationConfig } from '@angular/core';
import { provideNgDynamicJsonForm } from 'ng-dynamic-json-form';
export const appConfig: ApplicationConfig = {
...
providers: [
provideNgDynamicJsonForm({
customValidators: {
firstUppercase: firstUppercaseValidator,
}
}),
]
}
{
...
"validators": [
{
"name": "firstUppercase"
}
]
}
The way to use async validators is same with Custom validators.
import { ApplicationConfig } from '@angular/core';
import { provideNgDynamicJsonForm } from 'ng-dynamic-json-form';
export const appConfig: ApplicationConfig = {
...
providers: [
provideNgDynamicJsonForm({
customAsyncValidators: {
firstUppercase: firstUppercaseValidator,
}
}),
]
}
{
...
"asyncValidators": [
{
"name": "firstUppercase"
}
]
}