final-form / final-form

🏁 Framework agnostic, high performance, subscription-based form state management
https://final-form.org
Other
3k stars 213 forks source link

How to not run validation when registerField is called? #120

Open milesj opened 6 years ago

milesj commented 6 years ago

Are you submitting a bug report or a feature request?

Feature request?

What is the current behavior?

In my current implementation, fields are registered dynamically when React components mount, instead of being defined upfront. This works, but it seems like validation is run anytime a field is registered (https://github.com/final-form/final-form/blob/45f13bf4d7ae6dabfb7ab3c687f0c8dd8b3461c5/src/FinalForm.js#L656).

This is problematic as all fields are immediately marked as invalid upon mount, especially for validators like the following:

function validate(value) {
  if (!value) {
    return 'This field is required';
  }
}

Is there a way to handle this? Or work around it?

What is the expected behavior?

Not run validation on immediate mount/register.

What's your environment?

final-form v4.6.1 node 8.9.1 npm 5.6.0

milesj commented 6 years ago

I've tried working around this using touched, but it doesn't seem like that field is consistently updated.

miguelocarvajal commented 6 years ago

I am also in need of something similar. I need validation to occur on submission, not when the form is first loaded.

Initially thought about using the pristine variable but this is not available in the validate function.

erikras commented 6 years ago

For validation only on submission, you should use submission errors. There's no rule saying that they have to run on the server side. The normal validation is for running on every render to show errors in realtime.

hasan-aa commented 2 years ago

Having the same issue here. As a workaround I'm checking the meta.dirty flag before running the validations like:

const validate=(value, allValues, meta) => {
    if (meta.dirty) {
        return actualValidateFunction(value);
    } else {
        return undefined;
    }
};

Hope this helps.