DestroyedSoul

DestroyedSoul

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.

Showing Posts 1 to 4

al2o3cr

al2o3cr

This will make the button a plain HTML button, so clicking it will cause the form to “submit” and reload the page.

The usual culprit for this sort of behavior is that the LiveView that receives the event crashes. Is there anything else printed to the server log? Can you post the code of your handle_event?

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> HTML button element - HTML | 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>.

DestroyedSoul

DestroyedSoul OP

So, there were no crashes, nor any errors in the console.
It literally sent an event and then reloaded the live view.

handle_event had just one line {:noreply, socket}.

And @mindok was totally right: it was missing type="button". Next 1 button had it.
Thanks a lot!

loon

loon

damnnnnnnnittt!!! the type=“button” save my days, even AI couldn’t solve that. haha Thanks with love.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews