uniqueginun's avatar

Inject custom data on old helper

Hello everyone,

I am using Dropzone.js to upload files in my Laravel application and everything is working just fine, However, I have this little issue which is handling the validations exception response. what I want to do when I get this kind of response is to inject the old() helper with some custom variable I need to use in my JavaScript code to show thumbnails of previously uploaded files with Dropzone.

What I am currently doing now is whenever I select files I upload them temporarily in the 'storage/tmp/uploads' directory. and append hidden documents[] input contains uploaded file names coming from the server on each upload. so when I submit the form I move them to the desired directory.

However when I submit the form and get 422 response. I get the file names in old('documents'), I dont just want the file names. I want then name, size, type, and full storage path and I think I can do that inside my form request failedValidation function. but I don't know how!!

<script>
            const uploadedDocumentMap = {};
         
            Dropzone.options.myTargetInput = {
                url: '{{ route('data.storeMedia') }}',
                maxFilesize: 2,
                addRemoveLinks: true,

                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}"
                },

                uploadMultiple: false,

                init: function() {
                    const self = this;

                    @if (old('documents'))
                    
                        const mockFile = {
                           name: "C838MtoXoAEbnXh.jpg",
                           size: 90000,
                           type: 'image/jpeg'
                        };
                    
                        self.emit('addedfile', mockFile);
                        self.emit('thumbnail', mockFile, "http://127.0.0.1:8000/C838MtoXoAEbnXh.jpg");
                        self.emit('complete', mockFile);
                    
                        mockFile.previewElement.classList.add('dz-success');
                        mockFile.previewElement.classList.add('dz-complete');
                    @endif
                }
            };
        </script>

what I need is to get the variable mockFile which I currently hardcoded in the init function to get it from the server when I get a validation error response.

0 likes
1 reply
Snapey's avatar

You could manually run the validation, catch the validation exception, add the data to the error bag and rethrow the validation error?

Please or to participate in this conversation.