-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problems with File Upload field #448
Comments
@foxhound87 can you give a regular example of a simple file upload? I tried checking out the demo repository locally but it failed on @prontiol did you get this to work? |
@Dakkers Well, I made it work, but I had to implement custom |
@prontiol would you be willing to share this code? I was nearly pulling my hair out yesterday, which is a shame 'cause the rest of the library is superb. |
@Dakkers It will be too difficult to extract the working POC from the current project, as there are lots of custom code. |
ah ok. by "custom |
@Dakkers I inherited the mobx-form and overrode the |
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
Explanationhttps://stackoverflow.com/questions/1084925/input-type-file-show-only-button
|
I'm having really hard times trying to make
input[type=file]
play nicely withmobx-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:form.values()
does not contain files selected, so basically you have to implement your own method to collect all the valuesCan 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?The text was updated successfully, but these errors were encountered: