validations
can/observe/validationsThe can/observe/validations
plugin provides validations on observes. Validations
are set on can.Observe's static init
function.
The can/observe/validations
plugin provides validations on observes. Validations
are set on can.Observe's static init
function.
The following validates the
birthday
attribute in Contacts:Use errors
( [attrs...], newVal )
to read errors or to test if setting a value would create an error:Validation Methods
The most basic validate method is validate
()
.There are several built-in validation methods so you don't have to define your own in all cases like in the birthday example above.
(attrNames, options, proc)
Attributes validated with function.(attrNames, regexp, options)
Attributes match the regular expression.( attrNames, inArray, [options] )
Attributes are available in a particular array.(attrNames, min, max, [options])
Attributes' lengths are in the given range.( attrNames, [options] )
Attributes are not blank.(attrNames, low, hi, [options])
Attributes are in the given numeric range.Error Method
can.Observe.errors
()
runs the validations on this model. You can also pass it an array of attributes to run only those attributes. It returns nothing if there are no errors, or an object of errors by attribute.To use validations, it's required you use the observe/validations plugin.
Listening to events
Use bind to listen to error messages:
Demo
Click a person's name to update their birthday. If you put the date in the future, say the year 2525, it will report back an error.