Skip to content

Commit

Permalink
Skip confirm exit if form submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
onEXHovia committed Feb 27, 2025
1 parent 63233f4 commit b357595
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
15 changes: 14 additions & 1 deletion assets/js/controllers/confirm_exit_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Translation from '../core/translation';
export default class extends Controller {
static values = {
snapshot: String,
skip: Boolean,
};

static get shouldLoad() {
Expand All @@ -24,15 +25,27 @@ export default class extends Controller {
this.snapshotValue = this.snapshot;
}

skip() {
this.skipValue = true;
}

// eslint-disable-next-line consistent-return
confirm(event) {
if (this.snapshotValue !== this.snapshot) {
if (this.shouldConfirm) {
const message = Translation.trans('CONFIRM_EXIT');
event.returnValue = message;
return message;
}
}

get shouldConfirm() {
if (this.skipValue) {
return false;
}

return this.snapshotValue !== this.snapshot;
}

get snapshot() {
const params = new URLSearchParams(new FormData(this.element));
return params.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/app.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/Resources/views/CRUD/base_edit_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
method="POST"
{% if not sonata_config.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
{{ stimulus_controller('sonata-confirm-exit') }}
{{ stimulus_action('sonata-confirm-exit', 'confirm', 'beforeunload@window') }}
{{
stimulus_action('sonata-confirm-exit', 'confirm', 'beforeunload@window')|
stimulus_action('sonata-confirm-exit', 'skip', 'submit')
}}
{% block sonata_form_attributes %}{% endblock %}
>

Expand Down

0 comments on commit b357595

Please sign in to comment.