belgoros
I followed the tips suggested by @chrismccord in this PR to display some radio buttons as follows:
def render(assigns) do
~H"""
<p>Hello from radio buttons</p>
<.form for={@form}>
<div class="flex gap-x-2">
<%= for color <- ~w(gray red yellow green blue indigo pink purple) do %>
<.radio_group field={@form[:color]}>
<:radio value={color}><%= color %></:radio>
</.radio_group>
<% end %>
</div>
<input type="submit" value="Save" />
</.form>
"""
end
what raises some errors in the browser console saying:
Multiple IDs detected: color-0. Ensure unique element ids.
I added the radio_group component function to the core_componenets.ex module as follows:
attr :field, Phoenix.HTML.FormField, required: true
attr :rb_class, :string,
default: "rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6"
slot :radio, required: true do
attr :value, :string, required: true
end
slot :inner_block
def radio_group(assigns) do
~H"""
<div>
<%= render_slot(@inner_block) %>
<div :for={{%{value: value} = rad, idx} <- Enum.with_index(@radio)}>
<label for={"#{@field.id}-#{idx}"}><%= render_slot(rad) %></label>
<input
type="radio"
name={@field.name}
id={"#{@field.id}-#{idx}"}
value={value}
checked={to_string(@field.value) == to_string(value)}
class={@rb_class}
/>
</div>
</div>
"""
end
When inspecting the generated HTML, I can see that it does contain multiple IDs with the same value and looks like this:
<div>
<div>
<label for="color-0">red</label>
<input type="radio" name="color" id="color-0" value="red" class="rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6">
</div>
</div>
...
other radio inputs with the same `id="color-0"` value.
By the way, the above implementation does make it possible to provide any global attributes that might be updated.
Any ideas about that?
Trending in Questions
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Hermanverschooten
I think you need to move your
forto be only around the:radioslot, Now you are rendering the whole group for every color with only a single color, hence the index is always zero. BTW I would add anidto the.radio_groupand prefix that to the generated fieldid. If not the in the off-chance that you would need to add a second.radio_groupon the same page of the same field (in different forms) you still get duplicate id’s.belgoros
Nope, it will fit if I move the
forinside ofradio_group:The error:
LostKobrakai
With slots you need to use
:foras slots need to be direct children of their component. They cannot be wrapped in eex tags.belgoros
Yay, that was it, thank you!
Here is the full working version:
dimitarvp
Just a drive-by curiosity: why is
radio_groupwith the dot syntax andradiowith the colon one?LostKobrakai
One notation is for function components and the other for their nested slots.
dimitarvp
I have not kept up with the client-side Phoenix for a while, can you link me material that clarifies what do these two mean?
frankdugan3
dimitarvp
Ohhh I see, very nice actually. Thanks!
belgoros
The only trick would be to pass in a custom class to every radio button, compared to the outdated version:
Maybe, it will be easier with Petal, - Forms