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

时间:2014-05-31 14:14:43

标签: node.js validation 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
        },
    }
});

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

<template name="userSubmit">
    {{> quickForm collection="Users" id="insertUserForm" type="insert" validation="blur"}}
</template>

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

我该怎么做?

2 个答案:

答案 0 :(得分:6)

您可以覆盖架构的消息对象,如:

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

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

答案 1 :(得分:1)

解决方案as found by author

Users.simpleSchema().messages({
    "regEx zip": "Your Zip Code can only be numeric and should have 5 characters!",
});