Custom field validation message

I’m trying to figure out how to set a custom validation error message for a field.

I know it’s possible to set a general message by setting
error.validation.required
in the config or translation files.

Is it also possible to create a custom message for a field, e.g. like so?

email:
  label:
    de: E-Mailadresse
    en: email address
  type: email
  required: true
  error:
    de: Bitte geben Sie eine gültige E-Mailadresse an.
    en: Please enter a valid email address.

Using error for these messages does not work, neither does message nor validation. Maybe there’s an easy way in the blueprint?

Or would I have to go through each field and change the message, e.g. in the controller programatically?

It’s similar to what @neville is asking in Custom error message for required: true.

Thx!

It is currently not a supported use case to customize these error messages per field, only globally as you wrote via the translations.

The email field type has separate validations for required and for a valid email address.

I am afraid, you won’t be able to hack it by overwriting the field PHP config incl. validations itself, as the required validation is handled by the Kirby\Form\Field class prior to any other validation. Although looking at a code, maybe you could reset/overwrite it by a custom required validation in the field PHP config (kirby/config/fields/email.php at main · getkirby/kirby · GitHub). If you are not comfortable with extending/overwriting fields, you should not attempt this.

1 Like