Sha256: 2838c242d3754abf1be279a97bb57e04cd469719e810ddca0595178f27a61ec1
Contents?: true
Size: 887 Bytes
Versions: 1
Compression:
Stored size: 887 Bytes
Contents
class MaskValidator < ActiveModel::EachValidator # # This validator use the characters 'a', '9' and '*' # to validate the form of the content # # Example using in models: # validates :phone, mask: "(99) 9999-9999" # validates :acronym, mask: "***" # # Where: # a - Represents an alpha character (A-Z,a-z) # 9 - Represents a numeric character (0-9) # * - Represents an alphanumeric character (A-Z,a-z,0-9) # # TODO: refactoring def validate_each(record, attribute, value) return true if value.nil? || value.empty? definitions = { "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" } regex = /#{(options[:with].to_s.each_char.collect { |char| definitions[char] || "\\#{char}" }).join}/ match = value.match(regex) unless match && match.to_s == value record.errors.add(attribute, options[:message]) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mask_validator-0.1 | lib/mask_validator.rb |