chrisdel101

chrisdel101

Using liveview form to send data back to controller

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

Marked As Solved

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.

Also Liked

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.

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

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

Where Next?

Popular in Questions 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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement