romaluca

romaluca

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

Showing Posts 1 to 7

idi527

idi527

You need to use maps, I think.

select(form, :country, %{"Europe" => ["UK", "Sweden", "France"], …})

produces

<select id="user_country" name="user[country]">
  <optgroup label="Europe">
    <option>UK</option>
    <option>Sweden</option>
    <option>France</option>
  </optgroup>
  ...
</select>

So it would be something like

<%= select :user, :country_code, get_country_codes(), class: "form-control" %>

where get_country_codes/0 is defined in you view like

def get_country_codes do
  Countries.all()
  |> Enum.group_by(fn c -> c.name end, fn c -> c.alpha2 end)
end

Also, does Countries.all() hit a database? You might want not to do it, since this data is unlikely to change.

dreamingechoes

dreamingechoes

Maybe you can create a function in a view as a helper, something like:

  def country_options do
    Country
    |> Repo.all
    |> Enum.map(&{&1.name, &1.alpha2})
  end

and then use this function on the select tag in order to create the select options, like:

<%= select :user, :country_code, country_options(), class: "form-control", prompt: gettext("Select a country") %>
cleverlemming

cleverlemming

You can create a country Select control with all countries with the countries library as follows:

def get_country_codes do
      Enum.group_by(Countries.all, &(List.to_string(&1.region)), fn(x) -> {List.to_string(x.name), List.to_string(x.alpha2)} end )
      |> Enum.into( %{}, fn {key, list} -> {key, Enum.sort(list)} end)
end

Note that Countries.all returns single-quoted char lists, which causes errors from the Select control, which expects atoms, strings or integers, hence the cast to string. Using Enum.into sorts by the keys in the list.

There’s a lot to unpack with this if you’re new to Elixir. Hope it helps!

jung-hunsoo

jung-hunsoo

Here is the simple version.

def get_country_codes do
  Enum.into(Countries.all, %{}, fn x -> {x.name, x.alpha2} end) |> Enum.sort
end

Good luck!

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.

silverdr

silverdr

HI. Somewhat late to the party but… what happened to it?

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews