Sha256: 419a0cb357a7686b79290f18840aecf204c49a323d7d310a4de727cb2ab06000

Contents?: true

Size: 563 Bytes

Versions: 1

Compression:

Stored size: 563 Bytes

Contents

require 'active_model'

module ValidatesPhoneFormatOf
  Regexp = /\A\+?[1-9]\d{1,14}\z/
end

module ActiveModel
  module Validations
    class PhoneFormatValidator < EachValidator
      def validate_each(record, attribute, value)
        record.errors.add(attribute, :invalid_phone, **options.merge({:value => value})) if value.to_s !~ ValidatesPhoneFormatOf::Regexp
      end
    end

    module HelperMethods
      def validates_phone_format_of(*attr_names)
        validates_with PhoneFormatValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
validates_phone_format_of-2.0.1 lib/validates_phone_format_of.rb