How to validate data with DHTMLX Touch?
Data validation can be applied to:
It's performed by means of the property 'rules' - hash code of the rules (functions) for validated fields.
A code snippet below shows a common structure of the parameter:
rules:[{ control_id:rule}]
There are three means to set the desired rule:
There are 2 predefined rules expressions:
dhx.ui({ view:"form", elements:[{ view:"text", label: 'Name', id: "fname" }], rules:[{ fname:dhx.rules.isNotEmpty}] // the predefined rule 'isNotEmpty' });
Related sample: 02_form/05_validation.html
If you need, you can use any custom function as a rule. Such function will take 3 parameters (btw, value=obj[key]):
dhx.ui({ view:"form", elements:[{ view:"text", label:"Age", id:"age"}] rules:{age:isPositiveInteger} }); isPositiveInteger = function(value) {return (parseInt(value,10) == value && value>=0);}
Related sample: 02_form/07_validation.html
The keys're used in case you need to validate the whole data object or all set of data.
var dp = new dhx.DataProcessor({ rules:{$all:dhx.rules.isNotEmpty } ... });
dhx.ui({ view:"form", elements:[{ view:"text", label:"Password", id:"pass1", width:200}, { view:"text", label:"Confirm password", id:"pass2", width:200}], rules:{ $obj:confirmPassword } }); confirmPassword = function(obj) { return (obj.pass1 == obj.pass2);}
Please note, custom validation function for $obj takes the only parameter - a validated data object.
Related sample: 02_form/08_validation_keys.html