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

[Bug] Warn Before Leaving and Tabs #5370

Open
misiekch opened this issue Nov 9, 2023 · 1 comment
Open

[Bug] Warn Before Leaving and Tabs #5370

misiekch opened this issue Nov 9, 2023 · 1 comment
Assignees
Labels

Comments

@misiekch
Copy link
Contributor

misiekch commented Nov 9, 2023

Bug report

What I did

Selecting form tab causes showing warn about unsaved changes.

What I expected to happen

I wanted to leave the form without warning because i hadn't made any real changes.

What happened

Current tab is stored in hidden form field input[name='_current_tab'].

What I've already tried to fix it

Replacing getFormData method in form_content.blade.php

function getFormData() {
    return new URLSearchParams(new FormData(document.querySelector("main form"))).toString();
}

with

function getFormData() {
    var form = document.querySelector("main form");
    form.querySelector("input[name=_current_tab]").remove();
    return new URLSearchParams(new FormData(form)).toString();
}

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

@misiekch misiekch added the triage label Nov 9, 2023
@tabacitu
Copy link
Member

Hi @misiekch ,

Thanks for the report! I can confirm it's happening. For example if we turn 'warnBeforeLeaving' => true, in our Demo, in our MonsterCRUD you will get a notice when leaving, even if you didn't edit anything.

However... your code would remove the input entirely from the page. Which... 👀 I don't think is ok. Afaik the input is there in order for the "save and back" functionality to work, after the refresh to be redirected to the same tab. If you remove it with JS... that would stop working.

So a more "proper" solution would probably be to remove the input not from the DOM, but from the formData variable. And while we're at it, I believe we can expand the scope, and not only remove _current_tab but any input that starts with _ - those are just auxiliary inputs after all. So something like:

      function getFormData() {
        var form = document.querySelector("main form");
        var originalFormData = new FormData(form);
        var filteredFormData = new FormData();

        // Iterate over each entry in the original FormData
        for (let [key, value] of originalFormData.entries()) {
          // Add the entry to the new FormData if the key does not start with "_"
          if (!key.startsWith('_')) {
            filteredFormData.append(key, value);
          }
        }

        return new URLSearchParams(filteredFormData).toString();
      }

But that doesn't work either 🤦‍♂️ So... maybe that wasn't the problem here? At least not in Monster? Maybe there's a different input that gets populated with JS? 🤷‍♂️

I gotta go, but I'll investigate this further and come back with a solution.

Thanks for reporting.
Cheers!

@pxpm pxpm added Bug and removed triage labels Apr 3, 2024
@pxpm pxpm self-assigned this Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants