krayin / laravel-crm

Free & Opensource Laravel CRM solution for SMEs and Enterprises for complete customer lifecycle management.
https://krayincrm.com
Open Software License 3.0
9.95k stars 640 forks source link

File upload issue #1590

Open rajaevvotech opened 1 week ago

rajaevvotech commented 1 week ago

Hello.

I have used the file type to create a custom attribute. The file has been successfully updated in the admin portal lead form. however, while utilising the embed web form, the file has not been uploaded but, rest all field were updated. Kindly advice me to fix this issue.

I have tried to modify Ajax code too, but not working.

Ajax code path: packages/Webkul/WebForm/src/Resources/views/settings/web-forms/form-js.blade.php

Ajax code:

` $("#krayinWebForm").validate({ submitHandler: function(form) {

                document.querySelector('#loaderDiv').classList.add('loaderDiv');

                document.querySelector('#imgSpinner').classList.add('imgSpinner');

                $.ajax({
                    url: "{{ route('admin.settings.web_forms.form_store', $webForm->id) }}",
                    type: 'post',
                    data: formData,
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    dataType: 'json',
                    success: function (data) {

                        document.querySelector('#loaderDiv').classList.remove('loaderDiv');

                        document.querySelector('#imgSpinner').classList.remove('imgSpinner');

                        var validator = $("#krayinWebForm").validate();

                        if (data.message) {
                            $('.alert-wrapper .alert p').text(data.message);

                            $('.alert-wrapper').show();
                        } else {
                            window.location.href = data.redirect;
                        }

                        $("#krayinWebForm").trigger("reset");
                    },

                    error: function (data) {

                        document.querySelector('#loaderDiv').classList.remove('loaderDiv');

                        document.querySelector('#imgSpinner').classList.remove('imgSpinner');

                        var validator = $("#krayinWebForm").validate();

                        for (var key in data.responseJSON.errors) {
                            var inputNames = [];

                            key.split('.').forEach(function(chunk, index) {
                                if(index) {
                                    inputNames.push('[' + chunk + ']')
                                } else {
                                    inputNames.push(chunk)
                                }
                            })

                            var inputName = inputNames.join('');

                            var error = {};

                            error[inputName] = data.responseJSON.errors[key][0];

                            validator.showErrors(error);
                        }
                    }
                });
            }
        });
    });`

Path: packages/Webkul/Attribute/src/Repositories/AttributeValueRepository.php

if ($attribute->type === 'image' || $attribute->type === 'file') { try { if (request()->hasFile($attribute->code)) { $file = request()->file($attribute->code); if ($file->isValid()) { $data[$attribute->code] = $file->store($data['entity_type'] . '/' . $entityId); } else { throw new \Exception('Uploaded file is not valid.'); } } else { $data[$attribute->code] = null; } } catch (\Exception $e) { Log::error('File upload error: ' . $e->getMessage()); $data[$attribute->code] = null; // Handle file upload error gracefully } }

file

Thanks!

suraj-webkul commented 1 week ago

This issue has been fixed in this pr