Templating

The messages layer of indicative also let you define placeholders for runtime values. This is useful, when you want to have generic messages for certain validations.

const messages = {
 'required': '{{ field }} is required to create a new account'
}

The field will be replaced with the current field under validation. However, you can define specific messages for certain fields in combination with generic messages.

const messages = {
 'required': '{{ field }} is required to create a new account',
 'username.required': 'Please choose a unique username for your account'
}

Now every field under validation will use the required message apart from the username field, since we defined a specific message for it.

Available keywords.

Key Description

field

The name of the current field under validation. For nested arrays, it will be array.index.field. For Example: users.0.username.

validation

The name of the current validation.

argument

An array of arguments that a rule accepts.

const rules = {
  age: 'range:16,60'
}

const messages = {
  range: 'The {{ field }} must be over {{ argument.0 }} and under {{ argument.1 }} years'
}

Assinging closure

Also you can bind a closure as a custom message and return value will be used as the message string. This is useful, when you want to compute some values as runtime.

const messages = {
  range: function (field, validation, args) {
    return `The ${field} must be over ${args[0]} and under ${args[1]} years`
  }
}