Skip to content
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

Adding an error to form in onSubmit clears error on first try #617

Closed
j-castellanos opened this issue Nov 11, 2022 · 2 comments
Closed

Adding an error to form in onSubmit clears error on first try #617

j-castellanos opened this issue Nov 11, 2022 · 2 comments
Labels

Comments

@j-castellanos
Copy link

I am experiencing an issue where if a value is entered in a field and in onSubmit you invalidate the form, the error disappears. See the following code sandbox:

https://codesandbox.io/s/intelligent-sid-jsmp85?file=/src/App.js

import React, { useMemo } from "react";
import MobxReactForm from "mobx-react-form";
import { Observer } from "mobx-react";

export default function App() {
  const form = useMemo(
    () =>
      new MobxReactForm(
        {
          fields: {
            name: {
              label: "name"
            }
          }
        },
        {
          hooks: {
            onSubmit: (_form) => {
              // simulate a quick api call
              setTimeout(() => {
                // if a value was passed in for name then this
                // does not work
                _form.invalidate("i am an error");
              }, 100);
            }
          }
        }
      ),
    []
  );

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Observer>{() => <input {...form.$("name").bind()} />}</Observer>
      <Observer>{() => form.error && <p>{form.error}</p>}</Observer>
      <button onClick={form.onSubmit}>Submit</button>
    </div>
  );
}

If you wait long enough or hit submit again after the first time, then it works.

However the api call that I make completes quickly and I have this problem on the first try. If no value is entered for the input then the issue does not happen.

@j-castellanos
Copy link
Author

Ok this seems to be related to the field blurring

@foxhound87
Copy link
Owner

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants