Skip to main content

Customizing Error Messages

If needed you may provide custom error messages that a validator instance should use instead of the default error messages provided by Simple Body Validator.

Specifying Custom Message for a Rule

There are two ways to specify custom messages. First you may pass them as third argument to the make method.

    const validator = make(data, rules, {
required: 'The :attribute field is required.'
});

In this example the :attribute placeholder will be replaced by the actual name of the field under validation. You may also utilize other placeholders in validation messages. For example:

    const messages = {
'same': 'The :attribute and :other must match.',
'size': 'The :attribute must be exactly :size.',
'between': 'The :attribute value is not between :min - :max.',
'in': 'The :attribute must be one of the following types: :values'
};

The second way to pass custom messages is by using the setCustomMessages method.

    const validator = make(data, rules).setCustomMessages(messages);

Specifying a Custom Message for a Given Attribute

Sometimes you may wish to specify a custom error message only for a specific attribute. You may do so by using the dot notation. Specify the attribute's name first, followed by the rule.

    const messages = {
'email.required': 'We need to know your email address!'
};

Specifying Custom Attributes Values

Many of Simple Body Validator built in error messages include an :attribute placeholder that is replaced with the name of the field or attribute under validation. To customize the values used to replace these placeholders for specific fields, you may pass an object of custom attributes as the fourth argument to the make method or pass the object to the setCustomAttributes method.

    // pass the object as the fouth the attribute
const validator = make(data, rules, messages, {
email: 'email address',
})
    // use the 'setCustomAttributes' method to customize attribute value
const validator = make(data, rules).setCustomAttributes({
email: 'email address'
});