Refactor contact phone validation

#569
This commit is contained in:
Artur Beljajev 2017-08-05 19:21:32 +03:00
parent 9e8f806a43
commit 71b92afba3
2 changed files with 6 additions and 6 deletions

View file

@ -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? }

View file

@ -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