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

[0.5.x] Improve native browser submission while disabling formset on submit #112

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Dec 16, 2024

When using the Alpine library, it is common to continue using native browser form submissions.

A common pattern is to disable inputs on submit via a fieldset group.

<form
    x-data="{
        form: $form('post', '/register', {
            name: ''
        }),
    }"
>
    <fieldset
        x-bind:disabled="form.processing"
    >
        <input
            name="name"
            x-model="form.name"
            @change="form.validate('name')"
        >
    </fieldset>
    <button>Submit</button>
</form>

When we detect a native browser submission, via the formElement.addEventListener('submit') listener, we set processing = true, however because the event listener triggers before the browser has actually sent the request it disables the inputs BEFORE the submission and the form inputs are not sent to the browser.

Browsers do not submit disabled inputs.

Screenshot 2024-12-17 at 10 30 57

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled#usability

This PR delays setting processing = true until after the current tick to ensure inputs are sent before the form becomes delayed.

fixes #100

@taylorotwell taylorotwell merged commit 9430008 into main Dec 17, 2024
6 checks passed
@taylorotwell taylorotwell deleted the native-browser-submit-with-disabled branch December 17, 2024 15:18
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

Successfully merging this pull request may close these issues.

Disabling inputs prevents values being sent to server
2 participants