Home > Back-end >  Rails i18n ActiveModel: Translate error message for absence validation
Rails i18n ActiveModel: Translate error message for absence validation

Time:03-30

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?

CodePudding user response:

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"
  • Related