View Source EctoPhone (EctoPhone v1.1.0)

A custom Ecto type for phone numbers, with configuration for default country prefix and formatting style.

Usage

The EctoPhone type may be used in place of :string fields.

defmodule MySchema do
  use Ecto.Schema

  schema "table_name" do
    field :phone, EctoPhone
    field :phone, EctoPhone, default_prefix: 1
    field :phone, EctoPhone, format: :national
  end
end

Configuration

config :ecto_phone, default_prefix: 1, default_format: :national

EctoPhone may be configured at the application level with the following options.

  • :default_prefix: A country prefix to use when none is provided by users. Set to 1 if not configured by the application or by the field.
  • :default_format: The ex_phone_number format to use when printing phone numbers as strings. Defaults to :international. Valid options: :e164, :international, :national, :rfc3966

Summary

Types

  • e:e164
  • i:international
  • n:national
  • rfc:rfc3966
t()

Functions

Configures a specific field with init_opts/0.

Handles the sigil ~PHONE for an EctoPhone struct.

Types

@type format() :: :e164 | :international | :national | :rfc3966
@type format_modifier() :: charlist()
  • e:e164
  • i:international
  • n:national
  • rfc:rfc3966
@type init_opts() :: [default_prefix: integer(), format: format()]
@type t() :: %EctoPhone{e164: integer(), format: format()}

Functions

Configures a specific field with init_opts/0.

schema "table_name" do
  field :phone, EctoPhone
  field :phone, EctoPhone, default_prefix: 1
  field :phone, EctoPhone, format: :national
end
Link to this function

sigil_PHONE(number, modifiers)

View Source
@spec sigil_PHONE(binary(), format_modifier()) :: t()

Handles the sigil ~PHONE for an EctoPhone struct.

Modifers

  • e — format as :e164
  • i — format as :international
  • n — format as :national
  • rfc — format as :rfc3966