chrisdel101

chrisdel101

In LiveView how can I pass my data back to the controller? Non-liveview like Route.path_helper(conn, :new, user: @user, employee: @employee). I need the same map or keyword list as shown.

This is my latest iteration. I need those user and employee fields attached to each form, so I can’t just use html. Where can I put such a list of values here?

    <.form let={_f} for={:alert}  phx-submit={:alert}  #user: @user, employee: @employee>
        <%= submit "Alert1", class: "btn btn-primary" %>
        </.form>

Then in the controller. I need value to be a map (or whatever)

def handle_event("alert", value, socket) do
   IO.inspect(value) => {} # is currently an empty map. Only the first arg is correct.

I’ve asked on this question use form_for in liveview component without model or changeset. And this is similar to this too live_component inside of a form_for

Showing Posts 1 to 6

LostKobrakai

LostKobrakai

You can put the additional values in a hidden input on the form for them to be submitted with the rest of the forms data.

chrisdel101

chrisdel101 OP

Is that the orthodox way to do it?

Can U give an example? I can’t get it to work.

    <% IO.inspect(@current_employee.organization_id ) %> # 7
    <% IO.inspect(user.id ) %> # 2

     <.form let={_f} for={:alert}  phx-submit={:alert}>
          <%= hidden_input :organization_id, @current_employee.organization_id %>
          <%= hidden_input :user_id, user.id %>
        <%= submit "Alert", class: "btn btn-primary" %>
        </.form>

Give the error
no function clause matching in Phoenix.HTML.Form.generic_input/4

codeanpeace

codeanpeace

I’d suggest generating the hidden input via a Phoenix.Component rather than via Phoenix.HTML.Form assuming you’re running a recent version of LiveView.

<.input type="hidden" field={@form[:user_id]} value={user.id} />

Something like the above assuming you’ve set up the Phoenix.Component.form/1 properly.

Also, sending these ids via the form may be unnecessary if that information is already stored in your LiveView socket which is accessible in your event handlers e.g. def handle_event("alert", value, socket), do: ... end.

imkleats

imkleats

Unless your use-case is more complex than the example with other non-hidden inputs, it sounds like you might be better off using a phx-click binding instead of a form.

chrisdel101

chrisdel101 OP

Thanks everyone for the ideas!

Main problem solved:

I was missing alot of knowledge. Now I see the problem was the field didn’t work as I thought, i.e. automatically. I ended up just going with html after all, since yes, the rest of the details are still on the socket.

<button class="btn btn-primary" phx-click={:alert} value={user.id}></button>

def handle_event("alert", values, socket) do
    IO.inspect(values) # %{"value" => "1"}

Secondary problem: Re: the input component:

I didn’t know I needed to implement <.input/> myself. I’ve tried using the example here but I get undefined function attr/2. Phoenix 1.6 I think.

So I’m trying this, but I don’t know where it goes. In it’s own file? How do I use it? Since there is no render.

def input(assigns) do
  ~H"""
  <input type="text" name={@field.name} id={@field.id} value={@field.value} class="..." />
  """
end
codeanpeace

codeanpeace

These two are connected! On more recent versions of Phoenix, a core_components.ex file that implements <.input/> gets generated when creating a new app via mix phx.new my_app. That implementation uses pattern matching to automatically derive and add a :name and :value assign based on the :field assign. So if you want to use <.input/>, you’ll either need to manually add this file/function or implement it yourself.

https://github.com/phoenixframework/phoenix/blob/6095fb3aac9b37baa043374a0766680ab5d93a04/installer/templates/phx_web/components/core_components.ex#L277-L284

It can be a bit confusing at first, especially since the LiveView library includes a standard default .form component, but doesn’t include a standard default .input component. That’s why the autogenerated core_components.ex file includes a .simple_form component that wraps the standard .form component as well as an .input component.

— All posts loaded —

Where Next? Top

Trending in Questions Top

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
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews