Anber96

Anber96

I am working with forms, and i want to implement a custom select field. That is, a styled select with that displays and img tag (or a styled div) and the name of the selected option.

The liveview is the following:
lib/form_select_test_web/live/page_live.html.heex

<section class="prose">
  <%= f = form_for @changeset, "#", [phx_change: "update"], fn f -> %>
    <%= label(f, :assignee, class: "block text-sm font-medium text-gray-700") %>
    <%= select(f, :assignee, Enum.map(@users, & &1.name),
      class:
        "block w-full px-3 py-2 mt-1 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
    ) %>
  <% end %>
  <div>
    <.live_component
      module={CustomSelectComponent}
      id="sample-1"
      f={f}
      name={:assignee}
      options={@users}
    />
  </div>
</section>

It creates a form from a changeset and passes the form to a live component (the custom select).

lib/form_select_test_web/live/custom_select_component.ex

defmodule FormSelectTestWeb.Live.CustomSelectComponent do
  use FormSelectTestWeb, :live_component
  ...
  def update(assigns, socket) do
    %{f: f, name: name, options: options} = assigns

    value = "test" # how to get the selected value?
    selected_option = Enum.find(options, &(&1.name == value)) || List.first(options)

    {
      :ok,
      socket
      |> assign(assigns)
      |> assign(:selected_option, selected_option)
      |> assign(:name, name)
      |> assign(:f, f)
    }
  end

  def handle_event("toggle", _unsigned_params, socket) do
    {
      :noreply,
      socket
      |> assign(:expanded, !socket.assigns.expanded)
    }
  end
  ...
end

The problem i am having is how do i get the selected value from the form? I only get passed a {safe, form} value. But i want to access the form struct. What am i doing wrong?

For further references, i am using this post as inspiration:

Note: I am not using alpinejs, i want to implement it without the aplinejs part

Update:

If i move the livecomponet within the form tag

  <%= f = form_for @changeset, "#", [phx_change: "update"], fn f -> %>
    <%= label(f, :assignee, class: "block text-sm font-medium text-gray-700") %>
    <%= select(f, :assignee, Enum.map(@users, & &1.name),
      class:
        "block w-full px-3 py-2 mt-1 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
    ) %>
    <div>
      <.live_component
        module={CustomSelectComponent}
        id="sample-1"
        f={f}
        name={:assignee}
        options={@users}
        changeset={@changeset}
      />
    </div>
  <% end %>

i get the following error:

Showing Posts 1 to 5

andreaseriksson

andreaseriksson

That tutorial is a little outdated now. I will rewrite it. I can see that you are using the older way of implementing forms. Is this a new project?

Anber96

Anber96 OP

Yes! This is a new project

andreaseriksson

andreaseriksson

I think the error suggests that you should use new-ish formtag

<.form for={@form} />
  and stuff
</.form>
Anber96

Anber96 OP

That was the solution! I have now the form struct passed to the child live_component in which i can access the params.

Thanks for the help!

Anber96

Anber96 OP

Is there a possibility to get news of when the tutorial is updated? :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews