question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with File Upload field

See original GitHub issue

I’m having really hard times trying to make input[type=file] play nicely with mobx-react-form. It doesn’t work out of the box and native behavior is broken. When a file is chosen field.files property got updated, but:

  1. Native input is not updated (still shows: “No file selected”)
  2. Validation of the field is not called, so the form is still rendered invalid
  3. form.values() does not contain files selected, so basically you have to implement your own method to collect all the values

Can someone provide a very simple and limited example of the form with one required input[type=file] with form validation on file selection change and collecting the data for submit without picking data from the field itself?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Dakkerscommented, Nov 2, 2018

for anyone that sees this in the future, this is what I ended up doing:

Rendered component

        <div>
          <input
            {...field.bind()}
            className='display-none'
            type='file'
          />
          <button
            className='btn btn-primary'
            onBlur={field.onBlur}
            onFocus={field.onFocus}
            onClick={(e) => document.getElementById(field.id).click(e)}
            type="button"
          >
            <span>{btnText}</span>
          </button>
        </div>

Form initialization

    new Form({
      fields: [
        'image',
      ],
      initials: {
        image: null,
      },
      options: {
        image: {
          validateOnChange: true
        }
      },
      validators: {
        image: ({ field }) => {
          return [
            !!field.files && field.files.length > 0,
            'Required'
          ];
        }
      },
      hooks: {
        image: {
          onDrop: (field) => {
            field.validate({ showErrors: true })
          }
        }
      },
      types: {
        image: 'file'
      }
    }, {
      hooks: {
        onSuccess: this.submitForm
      },
      options: {
        uniqueId: () => `my-super-form-${shortid.generate()}`
      }
    });

Form submission



  * submitForm (form) {
    const data = form.values();
    console.log(form.$('image'));  // <-- this has a `.files` attribute which has the file.
  }

Explanation

https://stackoverflow.com/questions/1084925/input-type-file-show-only-button

image

  • I hide the default <input> tag using display: none (it is still rendered, technically, just hidden with CSS - so it has the field’s event handling)
  • I use a button that, when clicked, finds the hidden file input and triggers its onClick
    • the hidden file input has a unique ID generated by the shortid library
  • I access the file(s) I’ve uploaded by doing form.$('image').files
1reaction
prontiolcommented, Nov 2, 2018

@Dakkers It will be too difficult to extract the working POC from the current project, as there are lots of custom code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Most File Uploads Fail (And What to Do About It)?
Here we list a few common reasons that lead to file upload failure, and some options for solving these problems.
Read more >
Problem with file upload field - Jotform
Problem with file upload field ... Hi there,. Is there a reason the file loading widget at the end of this form is...
Read more >
File Upload Troubleshooting | Submittable Help Center
If you are having difficulties uploading files to a form you're trying to complete, please try the following troubleshooting steps:.
Read more >
NativeCloud File Upload Field Not Working
A small subset of NativeCloud users may experience problems with regard to using the Formstack Native File Upload field on their forms.
Read more >
Problems getting File Upload field to pass a JPG file to a CPT ...
All other fields are passing successfully into to their CPT counterparts ok, but the JPG file uploaded to the Forminator Form is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found