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

Saving does not submit FileFields #1852

Open
2 tasks done
lekoala opened this issue Nov 14, 2024 · 2 comments
Open
2 tasks done

Saving does not submit FileFields #1852

lekoala opened this issue Nov 14, 2024 · 2 comments

Comments

@lekoala
Copy link
Contributor

lekoala commented Nov 14, 2024

Module version(s) affected

4.x, 5.x

Description

Using FileField in the cms is not possible because files are not submitted due to how the form is submitted through ajax.

This is an issue because it means it's not always practical to "send files" not meant to be saved as assets, but for example as import templates or whatever.

Issue for reference:
lekoala/silverstripe-pure-modal#8

How to reproduce

Have a model admin instance and a model

Have this

    public function getCMSFields()
    {
         $fields = parent::getCMSFields();

        $fields->addFieldsToTab('Root.Upload', new FileField("TestFile"));
...

  public function onBeforeWrite()
    {
        parent::onBeforeWrite();

        dd($_FILES)

Select a file, click save and close, and see how $_FILES is empty

Possible Solution

i believe it's due to how the form is submitted by LeftAndMain see here

// get all data from the form
var formData = form.serializeArray();
// add button action
formData.push({name: $(button).attr('name'), value:'1'});
// Artificial HTTP referer, IE doesn't submit them via ajax.
// Also rewrites anchors to their page counterparts, which is important
// as automatic browser ajax response redirects seem to discard the hash/fragment.
formData.push({ name: 'BackURL', value: document.URL.replace(/\/$/, '') });

it should really be replaced by a simple FormData that should work fine instead

https://stackoverflow.com/questions/41684385/how-to-add-file-value-with-jquery-serialize

This should be really easy to fix, I'm happy to give it a try

Additional Context

No response

Validations

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)

PRs

@GuySartorelli
Copy link
Member

Thanks for reporting this.
FileField is a weird one - in the past I've given up and made my own complex uploading nonsense based around how UploadField works so it'd be nice to see this fixed.

This should be really easy to fix, I'm happy to give it a try

I'd love to review a PR

@lekoala
Copy link
Contributor Author

lekoala commented Nov 15, 2024

hey @GuySartorelli

here you go #1854

seems to work fine on my end!

i targeted 5.3 i don't think anyone expected this to work anyway, but this could also be easily back ported to older versions

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

No branches or pull requests

2 participants