imacrayon / alpine-ajax

An Alpine.js plugin for building server-powered frontends.
https://alpine-ajax.js.org
MIT License
631 stars 12 forks source link

Prevent `_self` from reloading the current page #84

Closed imacrayon closed 2 months ago

imacrayon commented 2 months ago

This PR adds some additional support for form validation to the special _self target keyword and introduces the new _top keyword.

The discussion surrounding these changes is here: https://github.com/imacrayon/alpine-ajax/issues/79

@gregmsanderson puts it best in regards to validating a form submission:

What we'd need is for the 302 caused by the validation error to target the form element (and so not cause a full-page reload) but the 302 caused by a successful submit to use _self and so do a full page load.

So the new behavior added to _self works like this:

<form id="my_form" x-target="my_form" x-target.3xx="_self">

For developers who want to trigger a full-page reload no matter what the status code is, we've added a new _top target key word that behaves just like _self did before this PR.

<form id="my_form" x-target="my_form" x-target.3xx="_top">

Fixes #79