Generates HTML <select> tags and option lists for the subdivisions of a territory — US states, Canadian provinces, French departments and so on.
Subdivision codes in CLDR carry their territory: California is :usca, not :ca. The territory is stripped from the option value, so a form receives "ca" and stores the subdivision as it is written in an address. The :full_codes option keeps the CLDR form where the value must remain globally unique.
Summary
Types
Subdivision type passed to a collator for ordering in the select box.
Functions
Returns a <select> tag of the subdivisions of a territory.
Returns the subdivisions of a territory as a list of {name, code} tuples.
Types
@type select_options() :: [ territory: atom() | binary(), locale: Localize.locale() | Localize.LanguageTag.t(), collator: function(), mapper: (subdivision() -> String.t()), selected: atom() | binary(), full_codes: boolean() ]
Subdivision type passed to a collator for ordering in the select box.
Functions
@spec select( form :: Phoenix.HTML.Form.t() | atom(), field :: Phoenix.HTML.Form.field() | atom(), select_options() ) :: Phoenix.HTML.safe() | {:error, Exception.t()}
Returns a <select> tag of the subdivisions of a territory.
Arguments
formis aPhoenix.HTML.Form.t/0.fieldis the field name inform.optionsis a keyword list of options.
Options
:territoryis the territory whose subdivisions are listed, for example:US. Required.:localeis any locale returned byLocalize.all_locale_ids/0. The default isLocalize.get_locale/0.:full_codeskeeps the CLDR subdivision code (:usca) as the option value rather than stripping the territory ("ca"). The default isfalse.:selectedis the subdivision to mark as selected. Accepts either form of the code.:collatororders the subdivisions. The default sorts by localized name usingLocalize.Collation.sort/2, so the order is correct for the locale.:mapperbuilds each option from asubdivision/0. The default is&{&1.name, &1.subdivision_code}.
Remaining options are passed to PhoenixHTMLHelpers.Form.select/4.
Returns
A
Phoenix.HTML.safe/0<select>tag, or{:error, exception}if the territory or locale is invalid.
Examples
Localize.HTML.Subdivision.select(:address, :state, territory: :US)
Localize.HTML.Subdivision.select(:address, :state, territory: :US, locale: :fr)
@spec subdivision_options(select_options()) :: [tuple()] | {:error, term()}
Returns the subdivisions of a territory as a list of {name, code} tuples.
Takes the same options as select/3.
Returns
A list of
{name, code}tuples, or{:error, exception}if the territory or locale is invalid.
Examples
iex> {name, code} = Localize.HTML.Subdivision.subdivision_options(territory: :US) |> hd()
iex> {name, code}
{"Alabama", "al"}