can-define-validate-validatejs
Adds validation methods and observables to a can-define/map/map using validate.js.
defineValidate(Map)
Checks for ValidateJS constraints and attaches useful methods.
import defineValidate from "can-define-validate-validatejs";
const User = DefineMap.extend( {
name: {
validate: {
presence: true
}
}
} );
// Attach methods to any instance created of `User`
defineValidate( User );
const user = new User();
user.errors();//-> [{name: ['is required']}]
Parameters
- Map
{Object}
:The can-define/map/map constructor. Adds errors and [can-define-validate-validatejs.test-set] methods to the prototype of this map.
Usage
Any validation properties must match the structure used by Validate.JS constraints.
For example:
const User = DefineMap.extend( {
name: {
validate: {
presence: true
}
}
} );
Initialize the validators on the Define Map by calling the defineValidate
function.
defineValidate( User );
When an instance is created, the instance will have validation properties that can be used in other modules or in templates
In a module:
const user = new User();
const onSubmit = function() {
if ( user.errors() ) {
alert( "Cannot continue, please check form for errors" );
}
};
In a template:
<input type="submit" disabled:from="user.errors()"/>