Class: CurrencyValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/flash_validators/validators/currency_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/flash_validators/validators/currency_validator.rb', line 3

def validate_each(record, attribute, value)
  strict_mode = options[:strict] || false

  if strict_mode
    format = /^\d+(\.\d{2})$/ # Strict: requires leading number and exactly two decimals, 1.45
  else
    format = /^\d*+(\.\d{1,2})$/
  end

  unless value =~ format
    record.errors[attribute] << (options[:message] || I18n.t('errors.messages.currency'))
  end
end