mobx-react-form icon indicating copy to clipboard operation
mobx-react-form copied to clipboard

Call to `form.values()` fails if fields are an empty object on submit

Open GonchuB opened this issue 7 years ago • 1 comments

The issue

When the fields object is empty, we get into this codepath: https://github.com/foxhound87/mobx-react-form/blob/139e2892f72be65455d26680765000f436e3a3fb/src/shared/Actions.js#L148 which in turn calls https://github.com/foxhound87/mobx-react-form/blob/139e2892f72be65455d26680765000f436e3a3fb/src/parser.js#L45

In this scenario, when submitting the form with empty fields, I get the following error:

olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/parser.js:162 Uncaught (in promise) TypeError: $field.$output is not a function
    at Object.parseCheckOutput (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/parser.js:162)
    at HookedForm.get (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/shared/Actions.js:265)
    at HookedForm.values (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/shared/Helpers.js:23)
    at HookedForm.onSuccess (olwyk85669.codesandbox.io/src/index.js:36)
    at HookedForm.Base.execHook (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/Base.js:67)
    at exec (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/shared/Actions.js:133)
    at eval (olwyk85669.codesandbox.io/node_modules/mobx-react-form/lib/shared/Actions.js:142)

How to reproduce

I could reproduce it in the following code sandbox: https://codesandbox.io/s/olwyk85669

Code to reproduce is the following:

import React from "react";
import ReactDOM from "react-dom";
import { Form } from "mobx-react-form";

class HookedForm extends Form {
  hooks() {
    return {
      onSuccess: form => console.log(form.values()),
      onSubmit: instance => instance
    };
  }
}

// Empty fields fail on submit
const emptyFields = {};
// These fields work, as they are not empty
const nonEmptyFields = { email: { type: "email" } };

const fields = emptyFields; // change to `nonEmptyFields` to make it work

function App() {
  const form = new HookedForm({ fields });

  return (
    <form onSubmit={form.onSubmit} className="App">
      <h1>Hello CodeSandbox</h1>
      <input type="submit" value="Submit" />
    </form>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Env

"dependencies": {
    "mobx": "5.8.0",
    "mobx-react-form": "1.35.2",
    "react": "16.5.2",
    "react-dom": "16.5.2",
    "react-scripts": "2.0.3"
  },

GonchuB avatar Jan 11 '19 13:01 GonchuB

:tada: This issue has been resolved in version 1.36.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

foxhound87 avatar Feb 09 '19 02:02 foxhound87