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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
You need to use maps, I think.
produces
So it would be something like
where
get_country_codes/0is defined in you view likeAlso, does
Countries.all()hit a database? You might want not to do it, since this data is unlikely to change.dreamingechoes
Maybe you can create a function in a view as a helper, something like:
and then use this function on the
selecttag in order to create the select options, like:cleverlemming
You can create a country Select control with all countries with the countries library as follows:
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
Here is the simple version.
Good luck!
kip
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.Territorywhich is a module that implements an HTMLselecthelper for use inPhoenix.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_cldruses “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:
Example in the “th” locale
Documentation
Arguments
Options
For select options see
Phoenix.HTML.Form.select/4Limitations
Currently the use of maps to define
optgroups is not supported.silverdr
HI. Somewhat late to the party but… what happened to it?
kip
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.