Sha256: 377af30ee6eee722fde238b52595de60f044dc9930e7184799d86d6921f52ccb

Contents?: true

Size: 451 Bytes

Versions: 3

Compression:

Stored size: 451 Bytes

Contents

class CurrencyValidator < ActiveModel::EachValidator

  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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flash_validators-1.1.0 lib/flash_validators/validators/currency_validator.rb
flash_validators-1.0.0 lib/flash_validators/validators/currency_validator.rb
flash_validators-0.0.1 lib/flash_validators/validators/currency_validator.rb