我有一个模型有 2 个属性。我先解释一下。
Backbone.Model.extend({
validation: {
username: [
{required: true, msg: 'Enter email/username.'},
],
password: [
{required: true,msg: 'Enter password.'},
{minLength:20,msg: 'Incorrect password length.'}]
},
});
我想在保存函数中验证单个属性。你有什么主意吗? 意思是,如果我的用户名和密码字段为空,则仅用户名会发生错误。
我正在使用backbone.validation有骨气。
谢谢
最佳答案
要使用 Backbone.Validation 验证单个或多个属性,请使用以下代码:
yourModel.isValid('username') // or
yourModel.isValid([ 'attribute1', 'attribute2' ])
关于javascript - 主干仅验证保存中的单个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244814/