Sha256: c825bb945dc4acc04739af093cd419203d262c7ade737b372861a44c1100c064
Contents?: true
Size: 1.08 KB
Versions: 1
Compression:
Stored size: 1.08 KB
Contents
# Checks validity of values supplied for an object's currency/cost attribute # # example usage: # validates :atrribute_to_check, :currency_format => true class CurrencyFormatValidator < ActiveModel::EachValidator # Compares object's currency/cost attribute against regex /^\d+(\.\d{2})?$/ for validity # @param [BasicRecord] record, This is the object needing validation # @param [Symbol] attribute, The attribute being validated, in this case :cost # @param [String] value, The value of the attribute to compare against the regex # @return [Boolean] True or false depending on if the value of the record's attribute is in the proper format 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
format_validators-0.0.8 | app/validators/currency_format_validator.rb |