Meteor autoform simple-schema:如何自定义验证消息?

nde*_*eau 3 validation node.js meteor meteor-autoform

我有这个架构:

Users = new Meteor.Collection('users', {
    schema: {
        firstname: {
            type: String,
            label: "First name",
            max:50
        },
        lastname: {
            type: String,
            label: "Last name",
            max:50
        },
        email: {
            type: String,
            label: "E-mail",
            regEx: SimpleSchema.RegEx.Email,
            optional: false,
            max:50
        },
        tel: {
            type: String,
            label: "Phone",
            optional: false,
            max:50
        },
        zip: {
            type: String,
            label: "Zip code",
            optional: false,
            regEx: /^[0-9]{5}$/,
            max:50
        },
        city: {
            type: String,
            label: "City",
            optional: false,
            max:50
        },
    }
});
Run Code Online (Sandbox Code Playgroud)

在我的模板中,我像这样使用Autoform:

<template name="userSubmit">
    {{> quickForm collection="Users" id="insertUserForm" type="insert" validation="blur"}}
</template>
Run Code Online (Sandbox Code Playgroud)

我想自定义Zip代码的错误消息.我没有"不遵守正则表达式",而是希望:"您的邮政编码只能是数字,应该有5个字符"

我怎样才能做到这一点?

小智 6

您可以覆盖模式的消息对象,如:

mySimpleSchemaInstance.messages({
  "regex": "Your Zip Code can only be numeric and should have 5 characters"
})
Run Code Online (Sandbox Code Playgroud)

查看更多:https: //github.com/aldeed/meteor-simple-schema#customizing-validation-messages