All the Angular’s built-in validators are supported.
The
Validators.email
but usingValidators.pattern
:/^[^@\s!(){}<>]+@[\w-]+(\.[A-Za-z]+)+$/
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, set the name
with value that match with the key inside customValidators
.
{
...
"validators": [
{
"name": "firstUppercase"
}
]
}
...
@Component({...})
export class YourComponent {
validators = {
firstUppercase: firstUppercaseValidator,
};
}
<ng-dynamic-json-form ... [customValidators]="validators"></ng-dynamic-json-form>
The custom validators can be provided in the providers, by using provideNgDynamicJsonForm()
, to make it becomes available globally.
import { ApplicationConfig } from '@angular/core';
import { provideNgDynamicJsonForm } from 'ng-dynamic-json-form';
export const appConfig: ApplicationConfig = {
...
providers: [
provideNgDynamicJsonForm({
customValidators: {
firstUppercase: firstUppercaseValidator,
}
}),
]
}