Class: TranslatablePresenceValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/forms/translatable_presence_validator.rb

Overview

A custom validator to check for presence in I18n-enabled fields. In order to use it do the following:

validates :my_i18n_field, translatable_presence: true

This will automatically check for presence for each of the ‘available_locales` of the form object (or the `available_locales` of the form’s organization) for the given field.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, _value) ⇒ Object



12
13
14
15
16
17
# File 'app/forms/translatable_presence_validator.rb', line 12

def validate_each(record, attribute, _value)
  available_locales_for(record).each do |locale|
    translated_attr = "#{attribute}_#{locale}"
    record.errors.add(translated_attr, :blank) unless record.send(translated_attr).present?
  end
end