如何实现Canjs的验证?我遇到了一些麻烦。在他们的指南页面上,他们这样做:
Contact = can.Observe({
init : function(){
// validates that birthday is in the future
this.validate("birthday",function(birthday){
if(birthday > new Date){
return "your birthday needs to be in the past"
}
})
}
},{});但是当我尝试这个的时候,我没有任何成功。
发布于 2013-07-27 03:39:55
应该能行得通。您只需要在您的观察实例(Fiddle)上使用.errors()检索验证错误:
var Contact = can.Observe({
init : function(){
// validates that birthday is in the future
this.validate("birthday",function(birthday){
if(birthday > new Date){
return "your birthday needs to be in the past"
}
})
}
},{});
var dave = new Contact({
name: 'Dave',
birthday: new Date(2024, 05, 05)
});
console.log(dave.errors());您可以在can.Map.validations documentation中找到完整的演练。
https://stackoverflow.com/questions/17882451
复制相似问题