2

我寻求这种性质的东西

//validation rules in model "User"
attributes: {
    age: {
        required: true,
        type: 'numeric'
    }
},    

//now in controller, i want to be able to do this
Recipe.validate({age: 'An invalid age because it is a string. I except a validation error as response'});

问题是,它不起作用..它抱怨 beforeValidate 不可用等

4

1 回答 1

2

您需要将回调传递给.validate

Recipe.validate({age: 'blah'}, function(err){
  if (err && err.invalidAttributes) {
    console.log(err.invalidAttributes); 
  } else {
    // model is valid
  }
});
于 2014-09-15T23:06:06.643 回答