Improved phone number validation

This commit is contained in:
Sergei Tsõganov 2022-01-04 14:00:01 +02:00 committed by Sergei Tsõganov
parent 564c711681
commit 6eaca805dc
4 changed files with 14 additions and 3 deletions

View file

@ -16,6 +16,8 @@ module Depp
['Birthday', 'birthday'] ['Birthday', 'birthday']
] ]
validates :phone, e164: true, phone: true
class << self class << self
attr_reader :epp_xml, :user attr_reader :epp_xml, :user
@ -144,6 +146,8 @@ module Depp
end end
def save def save
return false if !valid?
hash = { hash = {
id: { value: code }, id: { value: code },
postalInfo: { postalInfo: {
@ -166,13 +170,14 @@ module Depp
hash[:id] = nil if code.blank? hash[:id] = nil if code.blank?
create_xml = Depp::Contact.epp_xml.create(hash, extension_xml(:create)) create_xml = Depp::Contact.epp_xml.create(hash, extension_xml(:create))
data = Depp::Contact.user.request(create_xml) data = Depp::Contact.user.request(create_xml)
self.id = data.css('id').text self.id = data.css('id').text
handle_errors(data) handle_errors(data)
end end
def update_attributes(params) def update_attributes(params)
return false if !valid?
self.ident_country_code = params[:ident_country_code] self.ident_country_code = params[:ident_country_code]
self.ident_type = params[:ident_type] self.ident_type = params[:ident_type]
self.ident = params[:ident] self.ident = params[:ident]

View file

@ -56,7 +56,8 @@
.col-md-3.control-label .col-md-3.control-label
= f.label :email, t(:email) + '*' = f.label :email, t(:email) + '*'
.col-md-7 .col-md-7
= f.email_field :email, class: 'form-control', required: true = f.email_field :email, class: 'form-control', required: true,
pattern: "[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"
.form-group .form-group
.col-md-3.control-label .col-md-3.control-label

View file

@ -377,6 +377,11 @@ en:
activemodel: activemodel:
errors: errors:
models: models:
'depp/contact':
attributes:
phone:
invalid: "Phone number must be in +XXX.YYYYYYY format"
too_long: "Phone number is too long"
'depp/user': 'depp/user':
attributes: attributes:
base: base:

View file

@ -5,7 +5,7 @@ class E164Validator < ActiveModel::EachValidator
length_validator.validate(record) length_validator.validate(record)
format_validator = ActiveModel::Validations:: format_validator = ActiveModel::Validations::
FormatValidator.new(with: /\+[0-9]{1,3}\.[0-9]{1,14}?/, FormatValidator.new(with: /\A\+[0-9]{1,3}\.[0-9]{1,14}\z/,
attributes: attribute) attributes: attribute)
format_validator.validate(record) format_validator.validate(record)
end end