bvobart

bvobart

How do I prevent my Delete button from triggering a form submission?

Hi Elixir forum!

I have a function component to view, edit and delete an object from my database. This component obviously contains a form to perform and submit the edits that the user makes, but it also contains a Delete button to delete it, see the screenshot below.

Shortened for brevity, this is the implementation of that component:

<div class="...">
  <.form for={@form} phx-change="validate-changes" phx-submit="save-changes">
    <%!-- form inputs here --%>

    <footer class="flex justify-between items-center">
      <div class="flex gap-2">
        <.button
          variant="secondary"
          disabled={not changed?(@form)}
          phx-disable-with="..."
        >
          Save
        </.button>
        <.button {[{@return_method, @return_to}]}>Cancel</.button>
      </div>
      <.button
        variant="outline"
        class="btn-md btn-error text-error hover:text-base-100"
        phx-click="delete"
        phx-value-id={@job_request.id}
        data-confirm="Are you sure you want to delete this job request?"
      >
        <.icon name="hero-trash" class="size-5" />
      </.button>
    </footer>
  </.form>
</div>

My problem is that whenever I click the Delete button, it not only triggers the delete event like I want it to, but it also triggers the form submission, thus causing the save-changes event to be fired as well. How do I prevent that from happening so that only the delete event gets fired?

What I’ve tried so far:

  • Use phx-click with “delete”. Does not work, as described.
  • Use phx-click with JS.push(“delete”) |> JS.patch(@return_to). Does not work, same problem.
  • Tried putting the same props as what the Cancel button gets on the Delete button (effectively: patch={@return_to}), along with phx-click="delete". Does not work, the delete event is never triggered, only the patch is executed.
  • Moved the Delete button outside of the .form component, but then I couldn’t figure out how to neatly style the button to still be positioned in the same way the screenshot shows.
  • I read in the docs that phx-change can also be applied on individual elements rather than just the form, which would cause the form’s change event to be ignored and only the individual element’s change event to be fired. I thought maybe the same applies to phx-submit, but it does not: putting phx-submit=”delete” or something similar on the Delete button has no effect and the problem still persists.

Do you have any ideas for a good solution?

important to note:

  • the .button component was generated by Phoenix 1.8.1 when I created the project, I merely extended the variants and made it possible to extend the classes passed down into it.
  • the Cancel button works just fine and doesn’t trigger a form re-submission, because the generated .button component renders as a .link when given an href, navigate or patch prop.

Marked As Solved

garrison

garrison

<.button type="button">Delete</.button>

Also Liked

bvobart

bvobart

Thanks, type=button combined with phx-click={JS.push(“delete”) |> JS.patch(@return_to)} solves it! Now I feel stupid for not trying that earlier :sweat_smile: AI already gave me this answer but because type isn’t in the include list for attr :rest on the default generated button component, I thought it was just a bogus response.

For those who were just like me looking for an explanation as to why this works and is valid, see the MDN docs: <button> HTML button element - HTML | MDN

Last Post!

garrison

garrison

Apparently someone just popped by and added it one day, probably unaware that it was “supposed” to be a list of global attributes! TBH I don’t think the idea of the list being global-only is helpful anyway, there are a number of common attributes that I hate having to put in :include every time.

Where Next?

Popular in Questions Top

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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement