Jump to content

[solved] Help with AJAX loaded InputfieldFile in ProcessModule


bernhard
 Share

Recommended Posts

Hello everyone,

I'm having an issue with an ajax loaded file upload field in a processmodule. The file upload field itself works as expected when the field is not ajax loaded (see video): The file is stored as value in the inputfield and will be executed after form submission. But when I load the file upload inputfield in a fieldset that is ajax loaded, the behaviour is different: The file gets uploaded instantly and the JSON return is invalid:

IEveQfa.png

Does anybody know how I can modify this behaviour?

Actually... the instant ajax upload could be even better for my usecase: Upload the CSV, execute it on the backend, reload the grid with new data (without reloading the page which means I don't lose filters, selections etc).

Here is the video demonstrating the issue:

 

Link to comment
Share on other sites

30 minutes ago, bernhard said:

the JSON return is invalid

No instant solution, but did you check in dev tools both request and returned data? If the JSON parser complains the response starts with a <, it seems to receive some HTML.

Link to comment
Share on other sites

You can look in the dev tools network tab to see the actual result of the ajax call and the error, which is returned. Afaik ajax upload is only supported for pages though, as the page is a piece of data, where the file can be appended to (with a temp. flag). Everywhere else there's nothing you could append the file to for later processing.

Link to comment
Share on other sites

I don't know if you call an ___executeXYZ() function in RockCRM to get back your answer, but in one of my app, I have to send the answer from my ProcessModule with :

header('Content-Type: application/json');
return json_encode($data);

to avoid the first HTML char of the <!DOCTYPE html> string being returned.

Link to comment
Share on other sites

Thank you all for you quick answers!

Seems that I was unclear about the problem. The main problem is that the Inputfield behaves different wether it is loaded instantly or via AJAX. When loaded directly, the field does NOT upload the file instantly. When loaded via AJAX, the file IS uploaded instantly and throws an error. I'm not trying to find a quick solution, I'm trying to find a universal solution that works in both scenarios (because I'm developing a module that can be used anywhere and anyhow (as AJAX or non-AJAX).

So the main problem is that I don't know WHY the field behaves different and so I don't know HOW I can modify this behaviour!

2 hours ago, Autofahrn said:

No instant solution, but did you check in dev tools both request and returned data? If the JSON parser complains the response starts with a <, it seems to receive some HTML.

I'm a bit confused about this error. The network entry shows an empty 200 return, but when I set a breakpoint the responseText is some RockGrid markup: 

COz2BRt.png

So this error might have something to do with RockGrid. But I'm not worried about this error because the upload has to fail since there is no page to upload the file to. What I want to achieve is that the field does NOT upload the file instantly and only submits the file on form submit. Just like the PW module installer does.

2 hours ago, LostKobrakai said:

Afaik ajax upload is only supported for pages though, as the page is a piece of data, where the file can be appended to (with a temp. flag). Everywhere else there's nothing you could append the file to for later processing.

In general yes, but there is a way to upload files via InputfieldFile (see the module installer for example). You just need to set a destinationPath property and you can then process this file. This works just fine when the field is loaded non-ajax.

I hope I could explain it a little better now ? Thx for your help!

  • Like 1
Link to comment
Share on other sites

The endpoint for the AJAX request is .../crm/transactions/?InputfieldFileAjax=1 and if I try to open this site in a new tab I get a prompt "does not respond, do you want to wait...?" after some time. But that would be ok for me. Once more: I want the ajax loaded field to behave just like it does when it is non-ajax loaded.

1 hour ago, bernhard said:

The main problem is that the Inputfield behaves different wether it is loaded instantly or via AJAX. When loaded directly, the field does NOT upload the file instantly. When loaded via AJAX, the file IS uploaded instantly and throws an error.

foZJuVS.png

The Inputfield changes to "Demo.csv" but the file is NOT uploaded. That's how the field should work on ProcessModules. But it does not work like this when the field is AJAX loaded. Then the field behaves like it was on a regular ProcessPageEdit screen. And I don't know why this is the case. I can't find any differences in the markup or in the code.

Edit: Another difference is that the AJAX field shows the drag&drop span while the non-ajax field does NOT show it: MTUGvqy.png

I've no idea why that is the case..

Link to comment
Share on other sites

public function executeDemo() {
    $form = $this->modules->get('InputfieldForm'); /** @var InputfieldForm $form */
    $tmpDir = $this->files->tempDir('upload_csv');

    $f = $this->modules->get('InputfieldFile');
    $f->attr('id+name', 'upload_csv');
    $f->extensions = 'csv';
    $f->maxFiles = 1; 
    $f->descriptionRows = 0;
    $f->overwrite = true;
    $f->label = 'Upload CSV yyy';
    $f->icon = 'download';
    $f->destinationPath = $tmpDir;
    $f->collapsed = Inputfield::collapsedYesAjax;
    $form->add($f);

    return $form->render();
}

Simple process module to replicate this issue. Comment the $f->collapsed line and it will work, leave it there and it will upload the file instantly (which is not wanted)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...