Validators are a mechanism for checking the validity of a model. Note that validators, like everything else in templates, bind to data, not user interface. The controls themselves keep track of the correctness of the data and display corresponding error messages.
The object describing the validators is a regular JavaScript object. Property names are the path to the
data item in the model (the object to be checked). If an array is encountered in the data path, then the
corresponding property is marked with the []
suffix. For example, a validator for the
Sum
property of a document row will have the name Document.Rows[].Sum
.
A validator value can be either a single validator or an array of validators. If it is an array, then all validators from the array are applied.
Each validator (single, or array element) can be a string, function, or object of a given structure. The main type of validator is an object. The rest of the values are just shorthand for the object.
valid
- validation string (standard validator) or function.async
- optional. The validator is asynchronous (see below).
msg
- error message. The check function can return a string and
not a boolean value. In this case, the value of this property is ignored.
severity
- the seriousness of the error. The default is 'error'.
Affects the appearance of the message and can be used in code.
regExp
- 'regExp'
type regular expression for a standard validator.
applyIf
- function determining whether to apply a validator.
If the value of the valid
property is a string, then this is the standard validator. Possible values:
'notBlank'
- the value is not empty.'email'
- the value represents a valid (syntactically) email address.
'url'
- the value represents the correct (syntactically) web address.
'isTrue'
- the value is exactly true
.
'regExp'
- the value matches the regular expression. In this case, the 'regExp'
property must be set.
If the validator is a string, then this is a check of the value for emptiness. The string itself will be an error message. This is the equivalent of an object
If the validator is a function, then it is the equivalent of an object
valid(elem: IElement, value?: any): boolean | string | Promise;
elem
- item to which the validator is applied
val
- value being checked.
true
- if the value is correct. false
- if the value is incorrect. string
- an error message if the value is incorrect. This string will override the value of the 'msg'
property. If the validator is declared as a function, then it must always return a string (there is simply no msg
value in this case).
templateValidatorResult object
- an object that defines the error message (msg
)
and its seriousness (severity
).
Promise
- for an asynchronous validator. The promise must return a boolean value or a string.applyIf
Function format applyIf?: (elem: IElement, value?: any): boolean
elem
- item to which the validator is applied..
val
- value being checked.
true
- if the validator is active and needs to be applied.
Asynchronous validators are used when you need to contact the server to check the
correctness. Such a validator is marked with the async: true
property.
The validator function for an asynchronous validator must return a Promise
,
which will be either a boolean value or a string (just like a regular validation function).
An asynchronous validator usually uses the $asyncValid controller method.
Please note! Return values for asynchronous validators are cached to prevent over-hitting the server. In fact, the validator is called only once every time the values associated with it change.