From 71b92afba3d1e24a54f2acc8aa587420ed96b8be Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Sat, 5 Aug 2017 19:21:32 +0300 Subject: [PATCH] Refactor contact phone validation #569 --- app/models/contact.rb | 4 ++-- lib/validators/phone_validator.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 49687df73..392ad3b98 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -19,10 +19,10 @@ class Contact < ActiveRecord::Base accepts_nested_attributes_for :legal_documents - validates :name, :phone, :email, :ident, :ident_type, presence: true + validates :name, :email, :ident, :ident_type, presence: true validates :street, :city, :zip, :country_code, presence: true, if: 'self.class.address_processing?' - validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/, phone: true + validates :phone, presence: true, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/, phone: true validates :email, format: /@/ validates :email, email_format: { message: :invalid }, if: proc { |c| c.email_changed? } diff --git a/lib/validators/phone_validator.rb b/lib/validators/phone_validator.rb index d0bf94f1b..a2a91e9f7 100644 --- a/lib/validators/phone_validator.rb +++ b/lib/validators/phone_validator.rb @@ -2,11 +2,11 @@ class PhoneValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if record.errors[:phone].any? - splitted_phone = value.split('.') - country_code = splitted_phone.first - phone_number = splitted_phone.second + phone_parts = value.split('.') + country_code = phone_parts.first + subscriber_no = phone_parts.second - if zeros_only?(country_code) || zeros_only?(phone_number) + if zeros_only?(country_code) || zeros_only?(subscriber_no) record.errors.add(attribute, :invalid) end end