Kurisu

Kurisu

How can I add "disabled" attribute to a Phoenix.HTML.Form select prompt option?

I’m using materialize_css as view style in my templates.
So I would like to generate something like this:

<select class="" id="form_choice" name="form[choice]">
    <option value="" disabled selected>Choose a color</option>
    <option value="white">white</option>
    <option value="black">black</option>
    <option value="gray">gray</option>
</select>

but I’m not finding where I could add the attribute “disabled” for the option set with “prompt” key.

Please, how can I transform something like
select :form, :choice, [:white, :black, :gray], prompt: "Choose a color"
to have the attribute “disabled” set on the prompt option ?

In the docs I see I can use with select, options such as :key, :value, :disabled but it does not work for the prompt option.

Marked As Solved

Gazler

Gazler

Phoenix Core Team

Here’s an example from the docs:

select(form, :role, [[key: "Admin", value: "admin", disabled: true],
                     [key: "User", value: "user"]])
#=> <select id="user_role" name="user[role]">
    <option value="admin" disabled="disabled">Admin</option>
    <option value="user">User</option>
    </select>

Generates a select tag with the given options .

options are expected to be an enumerable which will be used to generate each respective option . The enumerable may have:

  • keyword lists - each keyword list is expected to have the keys :key and :value . Additional keys such as :disabled may be given to customize the option

Also Liked

LostKobrakai

LostKobrakai

Disabling the default option is usually done to force the user to select a different option. It’s kinda like setting required on input fields.

Kurisu

Kurisu

@Gazler
Thank you, finally I managed the options to work even with optgroups. Probably I was passing wrong keyword list to the select/4 helper.

Now I can use something like:

[
  [key: "Choose a color", value: "", disabled: true],
  set1: [
    [key: "White", value: "white"],
    [key: "Black", value: "black"],
    [key: "Gray", value: "gray"]
  ],
  set2: [
    [key: "Blue", value: "blue"],
    [key: "Red", value: "red"],
    [key: "Green", value: "green"]
  ]
]

or even shorter :slight_smile:

[
  [key: "Choose a color", value: "", disabled: true],
  set1: [:white, :black, :gray],
  set2: [:blue, :red, :green]
]

And with the following function I’m able able to better automate the whole thing. ^^

def collection(options, prompt \\ "Please choose an option", disabled \\ true),
    do: [[key: prompt, value: "", disabled: disabled]] |> Enum.concat(options)

The param “options” can be anything I would pass as options to select/4

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement