Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore formmethod when value is "dialog" #1866

Closed
SamDudley opened this issue Oct 6, 2023 · 3 comments
Closed

Ignore formmethod when value is "dialog" #1866

SamDudley opened this issue Oct 6, 2023 · 3 comments

Comments

@SamDudley
Copy link
Contributor

SamDudley commented Oct 6, 2023

Buttons support dialog as a valid value for the formmethod attribute.

MDN documentation

Given the following example:

<dialog>
    <form hx-post="/test">
        <button formmethod="dialog" name="foo" value="bar">Submit</button>
    </form>
</dialog>

htmx will issue a request with a method of DIALOG

There are a couple of issues with this:

  • DIALOG is not a valid request method
  • backend frameworks don't understand the request and the sent form data is ignored

Here is the line of code which causes this:

htmx/src/htmx.js

Lines 2960 to 2961 in 04250d5

var buttonVerb = getRawAttribute(submitter, "formmethod")
if (buttonVerb != null) {

As dialog is a valid formmethod attribute, but not a valid request method, I think htmx should ignore the formmethod in this case when creating the AJAX request.

I'm going to create a PR with these changes and add some tests, but I'm more than happy to discuss further.

@SamDudley
Copy link
Contributor Author

Here is my PR #1867.

@geoffrey-eisenbarth
Copy link

Isn't the whole point of <button action="submit" formmethod="dialog"> to not submit the form and close an open <dialog> modal?

The "Cancel" button includes the formmethod="dialog" attribute, which overrides the form's default GET method. When a form's method is dialog, the state of the form is saved but not submitted, and the dialog gets closed.

(source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog)

With this in mind, IMO htmx should not continue the request once it detects that buttonVerb is "dialog".

var buttonVerb = getRawAttribute(submitter, "formmethod")
if (buttonVerb != null) {
    // ignore buttons with formmethod="dialog"
    if (buttonVerb.toLowerCase() == "dialog") {
       // Do something here to halt the event
    } else {
        verb = buttonVerb;
    }
}

@michal-laskowski
Copy link

Try this solution
Note that you have to specify target on form.

 <dialog
                hx-on--load="event.target.showModal()"
                hx-on-close="event.target.remove()"
                hx-on--after-settle="event.target.close()"
            >
            <form hx-post="/test"
            hx-target="closest container"
            hx-trigger="submit">
            
                            <button
                                type="button"
                                formnovalidate
                                hx-on:click="event.target.closest('dialog').close()"
                            >
                                Cancel
                            </button>
                            <button type="submit">
                                Continue
                            </button>
            </form>
</dialog>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants