Class: LocalizedEachValidator Abstract
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- LocalizedEachValidator
- Defined in:
- lib/localized_each_validator.rb,
lib/localized_each_validator/version.rb
Overview
Subclass this validator to perform your specific validations.
An ‘EachValidator` that uses the translation table to build its error messages. Override the #valid? method to describe your validation conditions of your subclasses.
The error message translation lookups conform to the standard hierarchy of internationalization keys as described by the ‘ActiveRecord::Errors#generate_message` method. (See its documentation for more information.) The last portion of the translation key path is the error message key, and by default it is the name of the validator class (excepting “Validator”), underscored and demodulized. For example, an `EmailAddressValidator` subclass would use the `email_address` key within the normal ActiveRecord error key structure.
Constant Summary collapse
- VERSION =
"2.0.0"
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.error_key ⇒ Symbol .error_key(value) ⇒ Object
39 40 41 42 43 |
# File 'lib/localized_each_validator.rb', line 39 def self.error_key(value=nil) return @error_key || to_s.demodulize.sub(/Validator$/, "").underscore.to_sym unless value @error_key = value end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/localized_each_validator.rb', line 24 def validate_each(record, attribute, value) return if [:allow_nil] && value.nil? return if [:allow_blank] && value.blank? record.errors.add(attribute, [:message] || self.class.error_key) unless valid?(record, attribute, value) end |