Anber96

Anber96

Custom select field for form

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:

Marked As Solved

andreaseriksson

andreaseriksson

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

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

Also Liked

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?

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement