foxhound87 / mobx-react-form

Reactive MobX Form State Management
https://foxhound87.github.io/mobx-react-form
MIT License
1.09k stars 129 forks source link

form.values() doesn't return type="file" value #311

Closed trekinbami closed 7 years ago

trekinbami commented 7 years ago

Hi! Great work on the plugin!

I'm running a form with a type="file" (dropzone). Went through the example, and I've set up a Form class which extends MobxReactForm in which I have my onSuccess and onErrors methods. These give me access to the Form and gives me access to all of its properties and methods (https://foxhound87.github.io/mobx-react-form/docs/api-reference/form-methods.html)

Everything goes well. The .values() returns all the right values, except when I try to extrapolate the value of the file input. Is there some sort of special trick I'm missing, or isn't this possible?

Frontend (I've combined some files for readability (so don't mind the exports, haha):

import { AddTeacherFormConfig } from './AddTeacherFormConfig';

export default observer(() =>
  <form onSubmit={AddTeacherFormConfig.onSubmit}>
    <fieldset>
     <DropzoneDefault>
            <DropzoneBox
              field={AddTeacherFormConfig.$('image')}
              onDrop={AddTeacherFormConfig.onDrop}
            />
      </DropzoneDefault>
      <ButtonRed type="submit">Docent toevoegen</ButtonRed>
    </fieldset>
  </form>
);

export default observer(({ field, onDrop }) => {
  return (
    <Dropzone
      className="dz"
      activeClassName="dz--active"
      rejectClassName="dz--rejected"
      onDrop={onDrop}
    >
      {field.files && field.files[0].length
        ? <DropzonePreview field={field} />
        : <div>Go!</div>}
    </Dropzone>
  );
});

Config:

const AddTeacherFields = {
  image: {
    id: 'image',
    type: 'file'
  }
};

class AddTeacherForm extends MobxReactForm {
  plugins() {
    return {
      dvr: {
        package: validatorjs,
      }
    };
  }

  onDrop(field) {
    console.log('Your file', field);
  }

  onSuccess(form) {
    console.log('Teacher success! Form Values:', form.values());
  }

  onError(form) {
    console.log('Teacher form errors: ', form.errors());
  }
}

export const AddTeacherFormConfig = new AddTeacherForm({
  fields: AddTeacherFields
});
foxhound87 commented 7 years ago

You can retrieve all the files from the Field files property, in your case: form.$('image').files

trekinbami commented 7 years ago

Well, that was simple. Thank you!