jacknorman

jacknorman

Form submission inside child liveview

Hello everyone

I had a question to which I cant understand how to implement it.

I have a parent live view which renders multiple child liveviews on a button click. Each child liveview has a form with a seperate submit button. I can render the child liveviews at will on the button click action.

Now the problem is that whenever I try to submit the form in the child template, it calls the appropriate handle_event in the the child liveview but the params are empty. I tried setting up phx_submit in the place where i am declaring the form along but it didnt work.

I am attaching my code below. Only the form for the child template.
P.S. I am giving an id to every child template that is rendered using the live_render function.


<%= f = form_for :details, "#"%>

    <label class="form-control-label" for="title">Title</label><br>
    <%= text_input f, :title, [class: "form-control"] %>
    <%= error_tag f, :title %>
    <br>

    <label class="form-control-label" for="name">Name</label><br>
    <%= text_input f, :name, [class: "form-control"] %>
    <%= error_tag f, :name %>
    <br>

    <label class="form-control-label">Telephone number</label><br>
    <%= text_input f, :telephone, [class: "form-control"] %>
    <%= error_tag f, :telephone %>
    <br>

    <label class="form-control-label">Text caption</label><br>
    <%= textarea f, :text_caption, [class: "form-control"] %>
    <%= error_tag f, :text_caption %>
    <br>

    <label class="form-control-label">Banner</label><br>
    <%= text_input f, :banner, [class: "form-control"] %>
    <%= error_tag f, :banner %>
    <br>

    <label class="form-control-label pb-05" for="startTime">Start Time</label><br>
    <%= time_input f, :start_time, [class: "form-control"] %>
    <%= error_tag f, :start_time %>
    <br>
                    
    <div class="add-event-button add-event-button-inter" phx-click="add_new_telephone">
    <div class="add-event-button-text" ><%= submit "Add", class: "btn p-0"%></div>

Marked As Solved

Matsa59

Matsa59

Hello,

You should read Form bindings — Phoenix LiveView v0.20.2

You bind an element to send a message when it’s clicked. Instead you want to send a form to your live view. You have to use phx_submit: add_new_telephone in your form params and just set a submit button in your form.


<%= f = form_for :details, "#", [phx_submit:  "add_new_telephone"] %>
...

<%= submit "Add", class: "btn p-0"%>
</form>

Also when you set a form to a variable like you do, don’t forget to close the form :wink:

Also Liked

jacknorman

jacknorman

yeah learned that the hard way. I just moved around my form_for declaration and closed it properly and it worked.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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

We're in Beta

About us Mission Statement