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 the default locale of the form object (or the ‘default_locale` of the form’s organization) for the given field.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, _value) ⇒ Object



11
12
13
14
# File 'app/forms/translatable_presence_validator.rb', line 11

def validate_each(record, attribute, _value)
  translated_attr = "#{attribute}_#{default_locale_for(record)}".gsub("-", "__")
  record.errors.add(translated_attr, :blank) if record.send(translated_attr).blank?
end