can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
50 lines (39 loc) • 1.49 kB
Markdown
Map.validations.static.validate validate
can.Map.validations 1
The following example validates that a person's age is a number:
Person = can.Map.extend({
init : function(){
this.validate(["age"], function(val){
if( typeof val === 'number' ){
return "must be a number"
}
})
}
},{})
The error message can be overwritten with `options` __message__ property:
Person = can.Map.extend({
init : function(){
this.validate(
"age",
{message: "must be a number"},
function(val){
if( typeof val === 'number' ){
return undefined;
}
return false;
})
}
},{})
`observe.validate(attrNames, [options,] validateProc)`
{Array<String>|String} attrNames Attribute name(s) to to validate
{Object} [options] Options for the
validations. Valid options include 'message' and 'testIf'.
{function(*,String)} validateProc(value,attrName) Function used to validate each
given attribute. Returns nothing if valid and an error message
otherwise. Function is called in the instance context and takes the
`value` and `attrName` to validate.
`validate(attrNames, [options,] validateProc(value, attrName) )` validates each of the
specified attributes with the given `validateProc` function. The function
should return a value if there is an error. By default, the return value is
the error message. Validations should be set in the Constructor's static init method.
can.