Search code examples
node.jsvalidationmeteormeteor-autoform

Meteor autoform simple-schema: how to customize validation message?


I have this schema:

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

In my template, I use Autoform like so:

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

I would like to customize the error message for the Zip code. Instead of having "Doesn't comply with regular expression", I would like to have: "Your Zip Code can only be numeric and should have 5 characters"

How can I do this?


Solution

  • You can override the message object of your schema like:

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

    See more: https://github.com/aldeed/meteor-simple-schema#customizing-validation-messages