【问题标题】:Meteor autoform simple-schema: how to customize validation message?Meteor autoform simple-schema:如何自定义验证消息?
【发布时间】:2014-07-21 04:24:31
【问题描述】:

我有这个架构:

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>

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

我该怎么做?

【问题讨论】:

    标签: node.js validation meteor meteor-autoform


    【解决方案1】:

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

    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

    【讨论】:

    • 对不起,我的 Javascript 不是很好...我应该用什么替换 mySimpleSchemaInstance 吗?什么?如何确保此消息仅适用于邮政编码而不适用于具有正则表达式的其他字段?
    • 我用找到的解决方案更新了我的问题。谢谢。
    【解决方案2】:

    解决方案as found by author

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2012-12-12
      相关资源
      最近更新 更多