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

Best Practices for Warnings in Validate? #3

Open
shrugs opened this issue Aug 13, 2019 · 0 comments
Open

Best Practices for Warnings in Validate? #3

shrugs opened this issue Aug 13, 2019 · 0 comments

Comments

@shrugs
Copy link

shrugs commented Aug 13, 2019

Are there any known best practices for setting field warnings (using field data) within the validate or submit functions? In our case the warnings can only be generated after the lengthy 'validate' or 'submit' process, which we obviously want to run as infrequently as possible.

When setting warnings in the validate function, a render loop is triggered since calling a mutator automatically triggers re-validation [FinalForm.js#L268](https://github.com/final-form/final-form/blob/master/src/FinalForm.js#L268]

The only examples I find are doing warnings out-of-band, most likely to avoid this issue, but then we'd be running our validation function twice, and that's a hard pill to swallow.


my solution right now is something like

const warnings = useRef({});

const validate = useCallback((values) => {
  warnings.current.myField = null;
}, []);

const onSubmit = useCallback(
  async values => {
      // ... some long-running submit function...
      // notify the user, but allow login regardless
      if (noPermissions) {
        warnings.current.myField =
          'some sort of warning';
      }
  },
  []
);


return (
     <Input ...>
     {warnings.current.myField && (
        <Flex.Item as={ErrorText} className="mv1">
          {warnings.current.myField}
        </Flex.Item>
      )}
);

but I feel iffy about using the ref, but it's a happy accident that the form is always re-rendered after validation/submit, so the ref value is always shown correctly (in this specific case)

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

No branches or pull requests

1 participant