简体   繁体   English

流星自动成型自定义验证不起作用

[英]meteor autoform custom validation not reactive

I'm trying to use a custom validation function for a field defined in a simpleSchema, however the error message does not render on the field. 我正在尝试对customSchema中定义的字段使用自定义验证功能,但是错误消息不会在该字段上呈现。

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"}]);
    }
},

I've defined a custom error message for it 我已经为其定义了自定义错误消息

SimpleSchema.messages({numNotUnique: "This number has already been entered"});

When I submit the form I can confirm that the custom function executes, but nothing changes in the UI for that field indicating the error. 当我提交表单时,我可以确认自定义函数已执行,但是该字段的UI中没有任何更改指示错误。 The context name "addNumberForm" I got from the SimpleSchema.debug = true; 我从SimpleSchema.debug = true;获得的上下文名称“ addNumberForm” SimpleSchema.debug = true; setting and seeing what was thrown for other fields with default validation. 设置并查看使用默认验证向其他字段抛出的内容。

What am I missing here? 我在这里想念什么?

After much trial and error I've figured it out. 经过反复尝试,我已经弄清楚了。

The simpleSchema named context is only necessary if manually validating using simpleSchema by itself. 仅当使用simpleSchema本身进行手动验证时,才有必要使用simpleSchema命名上下文。 Autoform takes care of this, and the custom function can return a simple string that defines the error. 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
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM