Validation Meteor SimpleSchema:使用异步自定义验证防止表单提交
我使用中引用的示例实现了一个自定义验证函数,用于验证用户名的唯一性。在本例中,如果发现用户名已存在,则进行异步调用并显示自定义验证消息 有一条注释指出,如果所有表单字段都有效,则将提交表单,但是由于模式中指定的“unique:true”要求,用户创建将失败。以下是示例文档中代码的相关部分:Validation Meteor SimpleSchema:使用异步自定义验证防止表单提交,validation,meteor,simple-schema,Validation,Meteor,Simple Schema,我使用中引用的示例实现了一个自定义验证函数,用于验证用户名的唯一性。在本例中,如果发现用户名已存在,则进行异步调用并显示自定义验证消息 有一条注释指出,如果所有表单字段都有效,则将提交表单,但是由于模式中指定的“unique:true”要求,用户创建将失败。以下是示例文档中代码的相关部分: username: { type: String, regEx: /^[a-z0-9A-Z_]{3,15}$/, unique: true, custom: function () { if (
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/,
unique: true,
custom: function () {
if (Meteor.isClient && this.isSet) {
Meteor.call("accountsIsUsernameAvailable", this.value, function (error, result) {
if (!result) {
Meteor.users.simpleSchema().namedContext("createUserForm").addInvalidKeys([{name: "username", type: "notUnique"}]);
}
});
}
}
}
在我的例子中,我让代码在测试激活代码是否有效的地方工作,我甚至让界面显示错误,但是由于没有其他“模式”失败,表单提交,尽管响应无效。。。我是否需要手动阻止表单提交(即使用jQuery),或者SimpleSchema中是否有我应该使用的内容
activationCode: {
type: String,
label: "Activation Code",
max: 200,
min: 10,
regEx: /^(?=.*[A-Z])(?=.*\d).+$/,
custom: function() {
if (Meteor.isClient && this.isSet) {
Meteor.call("validateActivationCode", this.value, function(error, result) {
if (result && !result.isValid) {
Clients.simpleSchema().namedContext("signupForm").addInvalidKeys([{
name: "activationCode",
type: "notValid"
}]);
return false;
}
});
}
}
}
多谢各位