ndemoreau
ndemoreau

Reputation: 3869

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?

Upvotes: 3

Views: 5246

Answers (2)

Nathan Tuggy
Nathan Tuggy

Reputation: 2244

Solution as found by author:

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

Upvotes: 0

acmeira
acmeira

Reputation: 170

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

Upvotes: 6

Related Questions