【问题标题】:meteor autoform custom validation not reactive流星自动生成自定义验证不响应
【发布时间】:2015-11-24 23:07:19
【问题描述】:

我正在尝试对 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”,并查看默认验证为其他字段抛出的内容。

我在这里错过了什么?

【问题讨论】:

    标签: javascript meteor meteor-autoform


    【解决方案1】:

    经过多次尝试和错误,我想通了。

    只有在使用 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
        }
    },
    

    【讨论】:

    • 如何在自定义方法调用中进行验证?
    • 很好的答案,但是如果你想让一个字段从另一个字段失效,你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 2018-12-28
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2018-06-17
    相关资源
    最近更新 更多