Libby
Libby

Reputation: 1022

Rails i18n ActiveModel: Translate error message for absence validation

I have this validation in a service class:

class Users::Updater
  include ActiveModel::Validations

  validates(:frozen_identification_data, absence: { message: 'Identification fields cannot be changed at this time' } )
end

I'm trying to move this error message into a locales file:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            absence: "Identification fields cannot be changed at this time"

But when I reproduce the test case, the error message is Frozen identification data must be blank. I'm assuming absence is incorrect here, but I can't find any examples of this usage on Google. Does anyone know how to translate this validation?

Upvotes: 1

Views: 810

Answers (1)

Deepesh
Deepesh

Reputation: 6398

From rails guides: https://guides.rubyonrails.org/i18n.html#error-message-interpolation

For absence the key should be present:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            present: "Identification fields cannot be changed at this time"

Upvotes: 1

Related Questions