Sha256: 4ce6752f2fa1f26030d3c2a84cd0c6a29145bec82f1009fa431b38f9c66f6e08

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Citizenship
  module Rails
    module Validators
      class NifValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          Citizenship.valid_nif!(value, options) if value.present?
        rescue Citizenship::NIFError => e
          record.errors[attribute] << (options[:message] || e.message)
        end
      end

      class NibValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          Citizenship.valid_nib!(value, options) if value.present?
        rescue Citizenship::NIBError => e
          record.errors[attribute] << (options[:message] || e.message)
        end
      end

      class PhoneValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          Citizenship.valid_phone!(value, options) if value.present?
        rescue Citizenship::PhoneError => e
          record.errors[attribute] << (options[:message] || e.message)
        end
      end

      class EmailValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          Citizenship.valid_email!(value) if value.present?
        rescue Citizenship::EmailError => e
          record.errors[attribute] << (options[:message] || e.message)
        end
      end

      class ZipCodeValidator < ActiveModel::EachValidator
        def validate_each(record, attribute, value)
          Citizenship.valid_zip_code!(value) if value.present?
        rescue Citizenship::ZipCodeError => e
          record.errors[attribute] << (options[:message] || e.message)
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, Citizenship::Rails::Validators)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citizenship-1.0.0 lib/citizenship/rails/validators.rb