我试图对在simpleSchema中定义的字段使用自定义验证功能,但是错误消息不会在该字段上呈现。
num: {
type: Number,
label: "Number",
min: 1,
decimal: false, // unnecessary as this is default for Number, but for future reference
autoform: {
group: "Info",
defaultValue: function() {
//@TODO - default to next number for logged in user
return 5;
}
},
custom: function () {
Collection.simpleSchema().namedContext("addNumberForm").addInvalidKeys([{name: "num", type: "numNotUnique"}]);
}
},
我已经为其定义了一个自定义错误消息
SimpleSchema.messages({numNotUnique: "This number has already been entered"});
当我提交表单时,我可以确认自定义函数已执行,但是该字段的UI中没有任何更改指示错误。我从SimpleSchema.debug = true;
设置中获得的上下文名称“ addNumberForm”,并查看使用默认验证对其他字段抛出的内容。
我在这里想念什么?
经过反复试验,我已经弄清楚了。
仅当使用simpleSchema本身手动进行验证时,才有必要使用simpleSchema命名上下文。Autoform会解决这个问题,并且自定义函数可以返回定义错误的简单字符串。
num: {
type: Number,
label: "Number",
min: 1,
decimal: false, // unnecessary as this is default for Number, but for future reference
autoform: {
group: "Info",
defaultValue: function() {
//@TODO - default to next number for logged in user
return 5;
}
},
custom: function () {
// some check
return 'numNotUnique'; // return our error
}
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句