diff --git a/Gemfile b/Gemfile index 050c95ba0..0804ca4db 100644 --- a/Gemfile +++ b/Gemfile @@ -18,7 +18,6 @@ gem 'ransack', '~> 2.3' gem 'truemail', '~> 1.7' # validates email by regexp, mail server existence and address existence gem 'validates_email_format_of', '1.6.3' # validates email against RFC 2822 and RFC 3696 - # 0.7.3 is the latest for Rails 4.2, however, it is absent on Rubygems server # https://github.com/huacnlee/rails-settings-cached/issues/165 gem 'rails-settings-cached', '0.7.2' diff --git a/app/models/registrar.rb b/app/models/registrar.rb index dbdd7e8d3..2e2051542 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -29,14 +29,11 @@ class Registrar < ApplicationRecord validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 }, allow_nil: true - validate :forbid_special_code - attribute :vat_rate, ::Type::VATRate.new after_initialize :set_defaults - validates :email, email_format: { message: :invalid }, - allow_blank: true, if: proc { |c| c.will_save_change_to_email? } - validates :billing_email, email_format: { message: :invalid }, allow_blank: true + validate :correct_email_format, if: proc { |c| c.will_save_change_to_email? } + validate :correct_billing_email_format alias_attribute :contact_email, :email @@ -196,4 +193,18 @@ class Registrar < ApplicationRecord def vat_liable_in_foreign_country? !vat_liable_locally? end + + def verify_email_mx_smtp(field:, email:) + errors.add(field, :invalid) unless email.blank? || Truemail.valid?(email) + end + + def correct_email_format + verify_email_mx_smtp(field: :email, email: email) + end + + def correct_billing_email_format + return if self[:billing_email].blank? + + verify_email_mx_smtp(field: :billing_email, email: billing_email) + end end diff --git a/config/initializers/truemail.rb b/config/initializers/truemail.rb index efcecd508..337ac8605 100644 --- a/config/initializers/truemail.rb +++ b/config/initializers/truemail.rb @@ -28,7 +28,11 @@ Truemail.configure do |config| # Optional parameter. You can predefine default validation type for # Truemail.validate('email@email.com') call without with-parameter # Available validation types: :regex, :mx, :smtp - config.default_validation_type = :smtp + if Rails.env.production? + config.default_validation_type = :smtp + else + config.default_validation_type = :regex + end # Optional parameter. You can predefine which type of validation will be used for domains. # Also you can skip validation by domain. Available validation types: :regex, :mx, :smtp @@ -43,11 +47,6 @@ Truemail.configure do |config| # It is equal to empty array by default. # config.whitelisted_domains = [] - unless Rails.env.production? - config.whitelisted_domains = %w[bestnames.test goodnames.test example.com inbox.test mail.test - outlook.test invalid.test email.test] - end - # Optional parameter. With this option Truemail will validate email which contains whitelisted # domain only, i.e. if domain whitelisted, validation will passed to Regex, MX or SMTP validators. # Validation of email which not contains whitelisted domain always will return false.