UIKernel provides a handy tool to validate data,
so that the user can be notified of invalid input
before submitting a form or changes in an editable grid.
createValidator
Returns a builder that allows to define validation rules.
Parameters:
Type
Name
Description
String
serverValidationUrl
Optional. URI to send requests when validatorInstance.isValidRecord() is called. If this parameter is specified, validation will be performed remotely. If this parameter is omitted or requests cause Error 413, validation will be performed locally by means of validators specified below.
Function
xhr
Optional. function sending requests to the server when validatorInstance.isValidRecord() is called. By default is used built-in xhr function, but you can override it here.
Required. Function to validate the field. If the field is invalid, the function should return a string value with error text. Else(if the field is valid) the function should return nothing. rule may be your own function, or one of the predefined in the UIKernel validation functions
Function
rule2
Optional
Function
ruleN
Optional
rules arguments:
Type
Name
Description
Any
value
The value of the field to validate
fields
Specify a rule for synchronous validation of a group of fields.
Parameters:
Type
Name
Description
String [ ]
fieldNames
Required. Field names(Array of string values) to add validation for.
Function
rule
Required. Function to validate the fields. If any of the fields is invalid, the rule function should add the corresponding error to the errors object(second argument passed to the rule function).
rules arguments:
Type
Name
Description
Object
record
Object with values of all fields. The object has the following structure: {field1: field1Value, field2: field2Value, …}
ValidationErrors
errors
Object with errors of all fields. If any of the fields is invalid, the rule function should add the corresponding error to this errors object.
asyncDependence
Specify server validation dependencies in a client validator.
Parameters:
Type
Name
Description
String [ ]
fieldNames
Required. Field names(Array of string values) which need to be validated on a remote server.
Example:
/validation/client.js
/validation/server.js
asyncField
Add an asynchronous validation rule to a field.
Parameters:
Type
Name
Description
String
fieldName
Required.
async Function
rule
Required. Async function to validate the field. If the field is invalid, the function should resolve with a string value of the error text. Else(if the field is valid) the function should resolve with an empty value(null or undefined).
asyncFields
Specify a rule for asynchronous validation of a group of fields.
Parameters:
Type
Name
Description
String [ ]
fieldNames
Required. Field names(Array of string values) to add validation for.
async Function
rule*
Required. Async function to validate the field. If any of the fields is invalid, the rule function should add the corresponding error to the errors object(second argument passed to the rule function) and resolve eventually.
rules arguments:
Type
Name
Description
Object
record
Object with values of all fields. The object has the following structure: {field1: field1Value, field2: field2Value, …}
ValidationErrors
errors
Object with errors of all fields. If any of the fields is invalid, the rule function should add the corresponding error to this errors object.
getValidationDependency
Get all dependent fields required for validation of passed fields.
Dependent fields come up when you use group validation methods: fields(), asyncDependence(), asyncFields().
Parameters:
Type
Name
Description
String [ ]
fields
Required. Fields names which you want to get dependent fields of
Returns: String [ ]
Example:
async isValidRecord
Checks validity of the given record. Returns ValidationErrors instance.
Parameters:
Type
Name
Description
Object
record
Required. Record to validate. The object has the following structure: {field1: field1Value, field2: field2Value, …}
Returns: Promise which resolves with ValidationErrors instance.
Built-in Validation Rules
There is a set of basic validation rules is provided out of the box:
Custom Validation Rules
You can also define your own validation rules. For example:
Validation Example
Usage of the validator is built-in in Grid and Form models,
here is an example
But you can also use it independently wherever you want, e.g.: