bvobart

bvobart

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.

Showing Posts 1 to 4

garrison

garrison

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

bvobart OP

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

garrison

garrison

Huh, you’re right. The docs explicitly state that the list of HTML global attributes is used but type is in there despite not being a global attribute.

That line in the docs should probably be updated!

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews