Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fercho's avatar

Repopulate a Dropzone Input if Request Fails

Im making a Post form with inputs along with a dropzone Div to upload Photos,

My issue is the following: As Im pushing all the photos to a session until the moment I persist the relation with the post, I dont know exactly how to proceed when some field is Not being filled. I need to go back and present the form again.

The problem is that all my dropzone photos are being emptied from the form but not from the session. so if I correctly fill the form again and submit the data I will have more photos persisted than they should.

Can anybody address me or making a suggestion please.

0 likes
17 replies
fercho's avatar

In laravel Docs there is nothing related with knowing about Requests being triggered.

michaeldyrynda's avatar

The problem you'll face is that if your request fails validation, you won't have uploaded the images - only their filenames.

Whilst you do have the filenames of files to be uploaded from session data, adding the mock objects to dropzone you won't be able to generate the thumbnails and so you'll only have a partial solution, which might look odd to end users if they then go and add more files - some will have thumbnails and others won't.

fercho's avatar
init: function () {
   var myDrzone = this;
    var mockFile = { name: 'images1.jpeg', size: 12345, type: 'image/jpeg'};
    myDrzone.emit("addedfile", mockFile);
    myDrzone.emit("thumbnail", mockFile);
    myDrzone.emit("success", mockFile);
      
    var existingFileCount = 1; // The number of files already uploaded
    myDrzone.options.maxFiles = myDrzone.options.maxFiles - existingFileCount;
      
    }

@deringer . Yea, I got that script added to my Dropzone and now it shows the previous selected Photo. Im not understanding you the part which you say "adding the mock objects to dropzone you won't be able to generate the thumbnails" Because the users needs to check which thumbnails they previously uploaded. So Having this names in a session

$file->move('posts/temp/',$name);
$request->session()->push('photos',$name);

when I persist the photos it will check for the given name in the given directory to make the relation.

michaeldyrynda's avatar

@fercho, sorry if I wasn't clear.

What I was saying is that if your form fails to validate, the files won't exist in posts/temp yet, so you won't be able to refer to them to pre-fill the dropzone form.

michaeldyrynda's avatar

Ah, so you're uploading the files before doing the validation? If so, to save yourself some trouble, I would do it the other way around personally.

jekinney's avatar

Another question few days ago about dropzone. Uploading before making an an article. Good answers and options on that thread.

fercho's avatar

@jekinney, Can you share the link you mention please? I would like to read it. @deringer. would you shed some idea how you would tackle this process?

Thank you both

michaeldyrynda's avatar

Dropzone is great if you use it as intended - upload files immediately on drop. If you're attaching those files to something else, you'd need some way of temporarily storing them until such time as you have their parent. In your case, if the the form fails to validate, you have to figure out what to do with those orphaned files - if that is repopulating the files from session, you then need to build in handling for this, so that you don't have the user upload the files again, but still move them from the temporary location.

If you can live without the asynchronous upload aspect it provides, you could probably just use regular file inputs. Keep in mind if you take this approach, your users will then upload the files before any validation takes place, meaning that you can run into the issue of validation failing and the user having to upload files again.

The simplest way to get around all of this, would be to only allow upload of files once the parent item is created; whether or not this is possible will depend on your application. In that way, you decouple creation of the parent item from uploading an attachment to it - which may be desirable.

fercho's avatar

I absolute agree with you @deringer Dropzone is great for the intended use, but I also think this way of preuploading the files before the post is made makes the users realize which photos they want and maybe before persisting, update or delete them. About validating the photos Dropzone has some rules which I think they are handy to prevent missbehavior. I ended up replacing Sessions with @Snapey 's suggestion setting a "hidden field (imageKey)." Thank you for the comments

1 like
sid405's avatar

@fercho Eventually everyone has hit this snag. You either temporarily assign the uploads a bogus id and go update it after article creation OR

and this is the approach i've been taking On the create view i don't show dropzone. Just the other fields. The user creates the resource, i redirect to the Edit view, flash a success message. There they can use dropzone.

That's it.

fercho's avatar

Thank you for comment@sid405 I took the first approach because I dont see the logic to return the user to keep filling fields. It is like the porpouse of ajax. To present the user a one time only page.

sid405's avatar

Domain specific considerations prevail :D

Snapey's avatar

@quangtran1301 can't change the laws of physics

You can though win "archaeologist of the day" award for dragging up a 7-year old thread.

1 like

Please or to participate in this conversation.