Best practice for submit from several submit buttons in LiveView form

Hi, I’m looking for the best practice to implement a form with two submit buttons in LiveView. If we consider the following example:

<%= i = form_for @changeset, "#", phx_submit: "save" %>
    <%= submit "Save" %>
    <%= submit "Save and add another" %>
</form>

I would like to implement a different behavior (redirect to a different URL) when handling the “save” event, depending on which submit button was used.

At first I realised that adding name and value attributes to the submit buttons would pass these to the event handler as part of the form parameters but soon discovered that it wasn’t the case when using Chrome.

The proper way to do this in pure HTML forms seems to be to add a “formaction” attribute to the submit buttons, which overrides the “action” attribute on the form, allowing for submitting the form to a different URL. I couldn’t find a way to make this work with my LiveView form when submitting is managed with phx_submit.

Is there a good way to achieve this or is it a behaviour currently not supported by phoenix_html?

Thanks :slightly_smiling_face:

2 Likes

Duplicate of https://elixirforum.com/t/live-view-2-submit-buttons-identify-which-one-was-pressed/27219 / https://github.com/phoenixframework/phoenix_live_view/issues/511

Doesn’t seem to be a reliable solution for this since the information of which button was used to submit is not propagated through the JavaScript layer LiveView relies on.