Sha256: 0d48cdc4d04584c49a53b213e0a266aba008848c67daf2c57edea1a364675855

Contents?: true

Size: 505 Bytes

Versions: 2

Compression:

Stored size: 505 Bytes

Contents

class CurrencyFormatValidator < ActiveModel::EachValidator
  def validate_each record, attribute, value
    strict_mode = options[:strict] || false
    
    format = /^\d*+(\.\d{1,2})?$/
    # Strict: requires leading number and exactly two decimals, 1.45
    format = /^\d+(\.\d{2})?$/ if strict_mode
    
    message = attribute.to_s.humanize + " must contain dollars and cents, seperated by a period"
    record.errors[attribute] << (options[:message] || message) unless value.to_s =~ format
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
format_validators-0.0.7 app/validators/currency_format_validator.rb
format_validators-0.0.6 app/validators/currency_format_validator.rb