Today I Learned

Custom validation error messages when using forms

Normally for rails model (i.e. Brand) you would do:

en:
  activerecord:
    errors:
      models:
        brand:
          attributes:
            subdomain:
              invalid: "should contain only 'a-z', '0-9' or '-' but not as starting or ending character"

When using validations in forms you need a small tweak:

  • activerecord => activemodel
  • brand => brand_form (assuming your form is BrandForm)

Final translation:

en:
  activemodel:
    errors:
      models:
        brand_form:
          attributes:
            subdomain:
              invalid: "should contain only 'a-z', '0-9' or '-' but not as starting or ending character"

Note: invalid is the key for validation type, full list here