VertexBuffer's avatar

How to check if an image has changed before submitting a form?

So I've got a form with fields as well as an image upload section (I'm using Laravel MediaLibrary to handle uploads), and the main problem I'm running into at the moment is if I go to edit a model on my frontend and just want to change say a text field of the model, it resubmits everything including the file and reuploads it.

Ideally I'd only want to reupload the file if there was actually a change to it such as it was removed or a new image replaced it.

I'm not really sure the best way to approach this so would appreciate some advice. Thanks.

0 likes
8 replies
Snapey's avatar

is the front end posting through ajax?

VertexBuffer's avatar

@snapey Yeah. I'm using InertiaJS. My post method is similar to the one in the PingCRM demo app ( https://github.com/inertiajs/pingcrm/ ) but I've tweaked it slightly.

        const data = new FormData();
        data.append('title', this.form.title || '');
        data.append('body', this.form.body || '');
        data.append('image', this.form.image || null);

        if (isEmpty(this.news)) {
          await this.$inertia.post(this.route('news.store'), data);
        } else {
          data.append('_method', 'put');
          await this.$inertia.post(this.route('news.update', this.news.id), data);
        }
Snapey's avatar

So can you clear the image field after submitting the form?

VertexBuffer's avatar

@snapey I'm not really sure what that has to do with it?

The main issue is, I'm using v-model on the file uploader.

When the props come in from inertiajs, I have to set the data variable to the image so it can be displayed to the user correctly. The issue is, there is no way for me to check if a NEW file has been uploaded or not.

Snapey's avatar

If its a file upload form element then when the form is submitted, presumably the whole form retains the previous values? If submit is pressed again then all the same values are re-submitted?

There is no way for me to check if a NEW file has been uploaded or not.

The file does not get 'uploaded' until the form is submitted, until that point its just a filename pointing to a local object. If, after submitting the form for the first time, you clear the file input then the only way the same image can be sent again is if the user re-selects the file?

I've re-read your question again and cannot see any other clue as to how your situation might be different to this?

artcore's avatar

If it's an edit I display the image in tag pointing to the file location. If there's a new upload to replace it there will be $request->files populated (instanceof UploadedFile) otherwise it's just the location which is the DB field. Without an upload.

If it's a create post there'll be no location yet, assuming you would store it, parse the filename etc and THEN have a known location which you persist.

Upload? Move file, parse filename and return the file location (separate method for reuse) persist object with existing location or new.

A bit more involved but if taken step by step should be managable

Please or to participate in this conversation.