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

View file

@ -56,7 +56,8 @@
.col-md-3.control-label
= f.label :email, t(:email) + '*'
.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
.col-md-3.control-label

View file

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

View file

@ -5,7 +5,7 @@ class E164Validator < ActiveModel::EachValidator
length_validator.validate(record)
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)
format_validator.validate(record)
end