vael-ui v0.5.7

Forms guide · 7 of 8

Translating errors

vael-form ships no translations. You connect your i18n once, add the messages to your own locale files, and errors follow a locale switch without validating again.

Connect your i18n formPlugin

Pass your vue-i18n instance to the plugin, in either mode, or any { t, locale } over your own catalog. Your schemas stay as they are.

// main.ts
import { createI18n } from 'vue-i18n'
import { formPlugin } from 'vael-form'

const i18n = createI18n({ legacy: false, locale: 'en', messages: { en, fr } })
app.use(i18n)
app.use(formPlugin, { i18n: i18n.global })

Errors in the reader’s language

These pages use their own catalog: switch the docs’ language in the header and the errors below change with it. Submit the form to see them.

The messages

Copy these keys into your locale files under formKit and translate the values. You don’t need all of them: the most specific key is tried first (tooSmall.string, then tooSmall.default), and a key you leave out shows the schema library’s own text. To change one message, write it in the schema: .min(8, 'Pick a longer name') shows as written.

{
  "formKit": {
    "required": "{field} is required",
    "invalidType": {
      "default": "{field} is not valid",
      "string": "{field} must be text",
      "number": "{field} must be a number",
      "int": "{field} must be a whole number",
      "boolean": "{field} must be true or false",
      "date": "{field} must be a valid date",
      "array": "{field} must be a list"
    },
    "tooSmall": {
      "default": "{field} is too small (minimum {minimum})",
      "string": "{field} must be at least {minimum} characters",
      "number": "{field} must be at least {minimum}",
      "array": "{field} needs at least {minimum} items",
      "date": "{field} must be on or after {minimum}"
    },
    "tooBig": {
      "default": "{field} is too large (maximum {maximum})",
      "string": "{field} must be at most {maximum} characters",
      "number": "{field} must be at most {maximum}",
      "array": "{field} can have at most {maximum} items",
      "date": "{field} must be on or before {maximum}"
    },
    "invalidFormat": {
      "default": "{field} is not in the right format",
      "email": "Enter a valid email address",
      "url": "Enter a valid URL",
      "uuid": "{field} must be a valid UUID",
      "regex": "{field} is not in the right format",
      "startsWith": "{field} must start with \"{prefix}\"",
      "endsWith": "{field} must end with \"{suffix}\"",
      "includes": "{field} must contain \"{includes}\""
    },
    "invalidValue": "Choose one of the available options for {field}",
    "notMultipleOf": "{field} must be a multiple of {divisor}",
    "invalidUnion": "{field} is not valid",
    "unrecognizedKeys": "Unrecognized fields: {keys}",
    "custom": "{field} is not valid"
  }
}

Field names in messages labels

Every message gets {field}: the label you give in labels, itself a key to translate, or the field’s name made readable, so firstName reads “First name”. Messages also get the rule’s values, such as {minimum} and {maximum}.

useForm({
  schema,
  labels: { email: 'labels.email', firstName: 'labels.firstName' },
})
// "{field} is required" becomes "Email is required", in the current locale

Your own messages msg

msg() makes a translated message from your own key, with values and a fallback. Register your keys and they complete in your editor. keyPrefix on the plugin moves the messages from formKit to a name of your choice.

import { msg } from 'vael-form'

z.string().refine(isFree, msg('errors.taken', { name }, 'Already taken'))

declare module 'vael-form' {
  interface Register {
    messageKeys: 'errors.taken' | 'errors.members'
  }
}