流星自动表单自定义验证不响应

meteor autoform custom validation not reactive

本文关键字:验证 不响应 自定义 表单 流星      更新时间:2023-09-26

我试图使用自定义验证函数在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中指示错误的字段没有任何变化。上下文名称"addNumberForm"我从SimpleSchema.debug = true;设置中获得,并查看其他字段抛出的默认验证。

我在这里错过了什么?

经过反复试验,我终于弄明白了。

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