An ecto type for phone numbers.
This library is part of the Synchronal suite of libraries and tools which includes more than 15 open source Elixir libraries as well as some Rust libraries and tools.
You can support our open source work by sponsoring us. If you have specific features in mind, bugs you'd like fixed, or new libraries you'd like to see, file an issue or contact us at [email protected].
{:ecto_phone "~> 1.0"}
EctoPhone
may be used in place of :string
fields, where extra
parsing and validation is desired.
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
Phone formats accept formats compatible with the ex_phone_number
library, and are used in the EctoPhone
implementations of
String.Chars
and Phoenix.HTML.Safe
:
:e164
- example:+14155555555
:international
- example:+1 415-555-5555
:national
- example:(415) 555-5555
:rfc3966
- example:tel:+1-415-555-5555
EctoPhone
also provides the ~PHONE
sigil for more concise creation
of phone structs.
iex> import EctoPhone, only: [sigil_PHONE: 2]
...>
iex> ~PHONE[1 415 555 5555]i
%EctoPhone{e164: 14155555555, format: :international}
EctoPhone
may be configured at compile time with the following values:
config :ecto_phone,
default_prefix: 1,
default_format: :international
- https://hex.pm/packages/ecto_phone_number — EctoPhone is heavily
inspired by
EctoPhoneNumber
. It's a great library and may provide for your needs. We needed some extra parameterization and error messages.