DestroyedSoul

DestroyedSoul

Button inside form reloads the entire view

Hello,
I have a page that consists of several user inputs and buttons.
For real-time validation (as well as updating character count) I am using form, but only phx-change, no phx-submit.

My page structure looks roughly like this:

<div class="max-w-5xl">
  <h1 tag here>
  <form phx-change="change_form">
    <%= if some_condition?() do %>
        <textarea> with some styling
       <button phx-click="event_one">Next 1</button>
    <% else %>
      <textarea> with practically the same styling
      <button phx-click="event_two">Next 2</button>
    <% end %>
  </form>
</div>

Pressing Next 1 button updates the state in such a way that some_condition would be fulfilled, and the second text area and Next 2 button would be displayed.
This works without any issues.

The Next 2 button is exactly the same as the first one, and after pressing it similar handle_event function is called.

The problem is that whatever I do (

  • handle_event does have some logic;
  • handle_event immediately returns {:noreply, socket};
  • Next 2 button does not have phx-click at all
    )
    pressing the button reloads the entire page.
    It does send phx-click event (except when I tried removing it), but then console immediately prints [info] GET /my-endpoint, the socket is reconnected, mount is called and the state of the page is reset.

I noticed one thing: if I remove this second button out of the form, it stops acting out. But no problem ever happens with the first button.

Please advise me on what am I missing here.

Marked As Solved

mindok

mindok

Further to @al2o3cr comments…

If you have a phx-click attribute on the button, but no matching handle_event it will crash the liveview and reload the page. You will see an error in the logs.

If you have a <button> with no type attribute, it will default to a form submit button if the button is contained within a form. See <button>: The Button element - HTML: HyperText Markup Language | MDN for more info.

Try setting the button types to button explicitly to override the default behaviour (e.g. <button type="button" phx-click="event_two">Next 2</button>.

Where Next?

Popular in Questions Top

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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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

Other popular topics Top

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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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, "eq...
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
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 47849 226
New

We're in Beta

About us Mission Statement