Class: PicoPhone::Rails::ValidationsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/pico_phone/rails/validations_controller.rb

Overview

region: always arrives as an already-resolved String -- no record lookup.

Instance Method Summary collapse

Instance Method Details

#validateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pico_phone/rails/validations_controller.rb', line 9

def validate
  phone = params[:phone].to_s
  return render json: { valid: false, blank: true } if phone.strip.empty?

  phone_number = PicoPhone.parse(phone, params[:region].presence)

  if phone_number.valid?
    render json: {
      valid: true,
      blank: false,
      e164: safe_phone_call(phone_number, :e164),
      national: safe_phone_call(phone_number, :national)
    }
  else
    render json: { valid: false, blank: false, message: I18n.t("errors.messages.invalid_phone") }
  end
end