I create a form in which there is a drag n drop area where the user can add files. I use react and when the user drop files on the area I add the files to input.value via JS. But when I want to submit the form, I get an error that I need to fill all fields. Everything works fine when I open a file through the file selection window.
The code that is executed if user drop files.
const form = document.getElementById('hubspotForm');
const fileInput = form.querySelector("input[name='files']");
if (fileInput) {
const list = new DataTransfer();
files.forEach(item => list.items.add(item));
fileInput.files = list.files;
}
Hey @VShvets are you attempting to submit a form via the froms apis? If so, the forms api actually doesn’t support file upload fields. You need to upload the file using the CMS files api and attach the url to your form submission as a field.
I’m using embedded forms. I created next to hubspot form a drag n drop area and check when user drop files there. Then I add to the hubspot form to the files field the selected files.
@VShvets - would love to know more about how you set this up. It would be a great improvment to the HubSpot file form options - when we can figure out the glitch
@SteveHTM , I have created a form with a file type field in hubspot. I used npm package to integrate the form into react application (https://www.npmjs.com/package/react-hubspot-form).
Since the form does not support drag n drop I created a custom area in react to which I added drop event handlers.
When user drop files the code in useEffect adds files to the field in the form. I checked in devtools and the files property really has files. But when I click the submit button I get an error that the field is empty (“Please complete this required field.”). However, if I click on the field and select the files through the modal window, everything works.
I also tested without the npm package but just with script, but it doesn’t work either.
@VShvets - I am wondering if, after you have manipulated the input field, whether you have to add an extra signal to “bubble” up the new value to the rest of the logic.
In a much simpler case of form manipulation that I constructed this looks like: