belgoros

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?

Showing Posts 1 to 10

Hermanverschooten

Hermanverschooten

I think you need to move your for to be only around the :radio slot, 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 an id to the .radio_group and prefix that to the generated field id. If not the in the off-chance that you would need to add a second .radio_group on the same page of the same field (in different forms) you still get duplicate id’s.

belgoros

belgoros OP

Nope, it will fit if I move the for inside of radio_group:

<.form for={@form}>
      <div class="flex gap-x-2">
        <.radio_group field={@form[:color]}>
          <%= for color <- ~w(gray red yellow green blue indigo pink purple) do %>
            <:radio value={color}><%= color %></:radio>
          <% end %>
        </.radio_group>
      </div>
      <input type="submit" value="Save" />

The error:

== Compilation error in file lib/live_draft_web/live/radio_buttons_live.ex ==
** (Phoenix.LiveView.Tokenizer.ParseError) lib/live_draft_web/live/radio_buttons_live.ex:25:13: invalid slot entry <:radio>. A slot entry must be a direct child of a component
    (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/tag_engine.ex:1376: Phoenix.LiveView.TagEngine.raise_syntax_error!/3
...
LostKobrakai

LostKobrakai

With slots you need to use :for as slots need to be direct children of their component. They cannot be wrapped in eex tags.

belgoros

belgoros OP

Yay, that was it, thank you!
Here is the full working version:

<.form for={@form}>
      <div class="flex gap-x-2">
        <.radio_group field={@form[:color]}>
          <:radio :for={color <- ~w(gray red yellow green blue indigo pink purple)} value={color}>
            <%= color %>
          </:radio>
        </.radio_group>
      </div>
      <input type="submit" value="Save" />
    </.form>
dimitarvp

dimitarvp

Just a drive-by curiosity: why is radio_group with the dot syntax and radio with the colon one? :thinking:

LostKobrakai

LostKobrakai

One notation is for function components and the other for their nested slots.

dimitarvp

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?

dimitarvp

dimitarvp

Ohhh I see, very nice actually. Thanks!

belgoros

belgoros OP

The only trick would be to pass in a custom class to every radio button, compared to the outdated version:

<div class="w-3/5 mb-6">
    <%= label f, :color, class: "block mb-2 text-sm" %>
    <div class="flex gap-x-2">
      <%= for color <- ~w(gray red yellow green blue indigo pink purple) do %>
        <label class="relative cursor-pointer" phx-target={@myself} phx-click="set_color" phx-value-color={color}>
          <div class={"inline-block w-8 h-8 #{color}-bg rounded-full"}></div>
          <%= radio_button f, :color, color, class: "hidden" %>
          <%= if @current_color == color do %>
            <span class="absolute z-10 inline-block w-4 h-4 text-white top-1 left-1/2 -translate-x-1/2">
              <i class="fas fa-check"></i>
            </span>
          <% end %>
        </label>
      <% end %>
    </div>
    <%= error_tag f, :color %>

Maybe, it will be easier with Petal, - Forms

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
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
yarrichar
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
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
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
rlipscombe
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
saleyn
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 Top

dominicletz
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
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
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
OndrejValenta
(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
kip
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews