question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent field validation on form mount

See original GitHub issue

bug report/feature request

What is the current behavior?

field validation runs on mount of form

<Field
  name="investorEmail"
  validateFields={[]}
  component={TextInput}
  type="email"
  withWrapper
  hint=""
  label="Investors e-mail address"
  validate={(value) => validateRequiredEmail(value, 'investorEmail', 3)}
/>

the field behaves correctly, when error is not collected through error={this.props.error || this.props.meta?.error} but only this.props.error, but I would like not to have these hacks in my code. Is there elegant solution?

What is the expected behavior?

prevent validation from running on the first render

can be acomplished by

validate={(value, values, meta) => meta.touched && validateRequiredEmail(value, ‘investorEmail’, 3)} , but then doesn’t validate on form submit

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pietrofxqcommented, Nov 25, 2020

I came here trying to find out how to prevent a FormSpy to call onChange on mount. I ended using lodash isEmpty together with the touched value of the form since it will always be an empty object on the first render to prevent saving the form

0reactions
bluSCALE4commented, Sep 17, 2020

I typically only validate if a value is present.

const validate = (value) => {
    if (value) {
      clearTimeout(validateTimeout);
      setIndicatorActive(true);
      console.log("validation executed");
      validateTimeout = setTimeout(() => {
        setIndicatorActive(false);
      }, 1000);
      if (!value || value.length < 2) {
        return "I need something longer 💁🏻‍♀️";
      }
    } else {
      return undefined;
    }
  };
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to disable validation on form reset react-final-form?
I have custom validation in email input field, which is being triggered by form.reset() , which reset the form (After user successfully ...
Read more >
Handling Forms - VeeValidate
This will prevent submitting the form until all fields are valid. Using validate(). You can validate the form without submissions using the validate()...
Read more >
useForm - unregister - React Hook Form
This method allows you to unregister a single input or an array of inputs. ... By unregister an input, it will not affect...
Read more >
Form - Ant Design
High performance Form component with data scope management. Including data collection, verification, ... When you need to validate fields in certain rules.
Read more >
Synchronous Validation Example - Redux Form
IMPORTANT: In your validate function, values can be undefined , so pay attention when you are validating nested fields. If not, you could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found