romaluca

romaluca

Select field with all country codes

I’m try to create a form with a select field with the list of all countries

<%= select :user, :country_code, Enum.map(Countries.all, fn c -> {c.name, c.alpha2} end), class: "form-control" %>

But it print this bad html:

<select class="form-control" id="user_country_code" name="user[country_code]"><optgroup label="Zimbabwe"><option value="90">90</option><option value="87">87</option></optgroup><optgroup label="Zambia"><option value="90">90</option><option value="77">77</option></optgroup><optgroup label="South Africa"><option value="90">90</option><option value="65">65</option></optgroup><optgroup label="Mayotte"><option value="89">89</option><option value="84">84</option></optgroup><optgroup label="Yemen"><option value="89">89</option><option value="69">69</option></optgroup><optgroup label="Samoa"><option value="87">87</option><option value="83">83</option></optgroup><optgroup label="Wallis and Futuna"><option value="87">87</option><option value="70">70</option></optgroup><optgroup label="Vanuatu"><option value="86">86</option><option value="85">85</o...

Why?
How can i do to have name like label and alpha2 (‘IT’, ‘EN’) like value of select field?
Thanks

Most Liked

kip

kip

ex_cldr Core Team

One of the lesser known corners of the ex_cldr universe is cldr_html. Its not as mature as the other libraries but I make progress on it when time permits. Today I added Cldr.HTML.Territory which is a module that implements an HTML select helper for use in Phoenix.

By default is returns a list of all known territories, using localised names. The default format is a Unicode flag followed by the standard name.

Background on why “territories” and not “countries”

A country is a political entity, a territory is a geographic one. For international localised applications, what constitutes a country can be a sensitive topic. Hence CLDR uses “region” or “territory”. ex_cldr uses “territory”.

Where CLDR data differs from ISO3166

Although very closely related, CLDR uses a combination of ISO3166 Alpha-2 codes and UN.M49 for geographic areas that enclose several territories.

Differences between ISO3166 name and CLDR names

As noted in TR35, CLDR takes a more pragmatic approach to territory names. Territory names may not match the official name of the territory, and the English or French names may not match those in ISO 3166. Reasons for this include:

  • CLDR favors customary names in common parlance, not necessarily the official names.
  • CLDR endeavors to provide names that are not too long, in order to avoid problems with truncation or overflow in user interfaces.

Example in the “th” locale

iex> Cldr.HTML.Territory.select(:my_form,  :territory, territories: [:US, :AU], 
       selected: :IT, locale: "th") |> safe_to_string()

<select id="my_form_territory" name="my_form[territory]">
  <option value="AU">🇦🇺 ออสเตรเลีย</option>
  <option value="IT" selected>🇮🇹 อิตาลี</option>
  <option value="US">🇺🇸 สหรัฐอเมริกา</option>
</select>

Documentation

Generate an HTML select tag for a territory list
that can be used with a `Phoenix.HTML.Form.t`.

Arguments

* A `Phoenix.HTML.Form.t()` form

* A `Phoenix.HTML.Form.field()` field

* A `Keyword.t()` list of options

Options

For select options see Phoenix.HTML.Form.select/4

* `:territories` defines the list of territories to be
  displayed in the the `select` tag.  The list defaults to
  the territories returned by `Cldr.known_territories/0`.

* `:style` is the format of the territory name to be used.
  The options are `:standard` (the default), `:short` and `:variant`.
  Not all territories have `:short` or `:variant` names in which
  case `:standard` is used for those territories.

* `:locale` defines the locale to be used to localise the
  description of the territories.  The default is the locale
  returned by `Cldr.get_locale/1`

* `:backend` is any backend module. The default is
  `Cldr.default_backend!/0`

* `:mapper` is a function that creates the text to be
  displayed in the select tag for each territory.  It is
  passed the territory definition as a map containing the keys
  `:territory_code`, `:name` and `:flag`.  The default function
  is `&({&1.flag <> " " <> &1.name, &1.territory_code})`

* `:selected` identifies the territory that is to be selected
  by default in the `select` tag.  The default is `nil`. This
  is passed unmodified to `Phoenix.HTML.Form.select/4`

* `:prompt` is a prompt displayed at the top of the select
   box. This is passed unmodified to `Phoenix.HTML.Form.select/4`

Limitations

Currently the use of maps to define optgroups is not supported.

kip

kip

ex_cldr Core Team

Um, nothing happened to it. Its still there and still supported.

Ahhhhh, I had the wrong link in the original post (fixed now). Its cldr_html.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement