logo dile-components dile-components

Messages and languages

The validation messages are available in English and Spanish, and you can customize them or add other languages.

Language of the messages

Every validation message of the Dile Components (the required message, the rules of @dile/validation and the fields' own errors, like an invalid amount in dile-input-money or an impossible date in dile-datepicker) chooses its language the same way. The first one found wins:

  1. The language property of the element (locale in dile-datepicker and dile-time-picker).
  2. The closest ancestor that is a DileForm with a language, or that has a lang attribute. Shadow roots are crossed, so the fields of a form use the language of the form.
  3. The default language of the application, set with setDefaultLanguage().
  4. The lang attribute of the document (<html lang="es">).
  5. English.

setDefaultLanguage() is useful when the application doesn't set <html lang>, or in tests:

import { setDefaultLanguage } from '@dile/ui/lib/language/language.js';

setDefaultLanguage('es');

The same module exports resolveLanguage(element, explicit) and resolveLanguageCode(element, explicit) ('es-ES' → 'es'), to use the same criterion in your own fields.

The CRUD components (dile-ajax-form, dile-crud-actions, dile-step-form) hand their language to the form when they get it, so the live validation and the validation before sending use the same language. English, the default language of those components, is not handed: the form keeps the language of the page.

Customizing the messages

Customize the messages of a form with its validationMessages property of the form, by rule or by field and rule. Placeholders like {min}, {max} or {other} are replaced with the rule parameters:

form.validationMessages = {
  required: 'Please fill in this field',
  minLength: 'At least {min} characters, please',
  'email.email': 'This email address does not look right',
};

To translate or change the messages of the whole application, or to add another language, use registerMessages():

import { registerMessages } from '@dile/validation';

registerMessages('fr', {
  required: 'Ce champ est obligatoire.',
  email: 'Saisissez une adresse e-mail valide.',
});

The message keys are the rule names, plus minLength.array and maxLength.array for arrays, decimal.exact for decimal when both parameters are equal (decimal:2) and invalid for the generic message.

The message of an error is the first one found in this order, both for the rules and for the required attribute of the fields:

  1. The message of the rule item in the schema, if any.
  2. The messages of setValidationSchema() and validationMessages (the former win on the same key), by field and rule: email.required. For array fields (items[], keys items.0, items.1...) also items.0.required, then items.required and items.*.required.
  3. The same messages, by rule: required.
  4. The messages of registerMessages() in the form language, then the default ones in English.

{label} is replaced with the label of the field, or its name, and {input} with its value, like :input in Laravel.

The fields' own errors (an impossible date, an incomplete mask...) can be customized in validationMessages too, by field and reason: { 'date.beforeMin': 'Too old.' }. See Field's own validation.

Help texts

Many fields show a help text with their message property, the same property used for the errors. When the error is cleared, the form shows the help text again. See Help texts.

Demo

A form in Spanish, with the lang attribute of a container, and a custom message for one rule.

<script type="module">
  import { LitElement, html } from 'lit';
  import { DileForm } from '@dile/ui/mixins/form';
  import { DileFormValidation } from '@dile/validation';
  import '@dile/ui/components/input/input.js';
  import '@dile/ui/components/button/button.js';

  class SpanishMessagesForm extends DileFormValidation(DileForm(LitElement)) {
    static validations = {
      email: 'email',
      nickname: 'minLength:4',
    };

    constructor() {
      super();
      this.validationMessages = {
        'nickname.minLength': 'Tu alias necesita al menos {min} caracteres',
      };
    }

    render() {
      return html`
        <dile-input name="name" label="Nombre" required></dile-input>
        <dile-input name="email" label="Email"></dile-input>
        <dile-input name="nickname" label="Alias"></dile-input>
        <dile-button @click=${() => this.validate()}>Validar</dile-button>
      `;
    }
  }
  customElements.define('spanish-messages-form', SpanishMessagesForm);
</script>
<div lang="es">
  <spanish-messages-form></spanish-messages-form>
</div>