流星自动形成自定义验证没有反应

meteor autoform custom validation not reactive

我正在尝试对 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 会处理这个问题,自定义函数可以 return 定义错误的简单字符串。

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
    }
},