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.

Trigger validation on both onChange and onBlur

See original GitHub issue

Feature request

What is the current behavior?

If true, validation will happen on blur. If false, validation will happen on change. Defaults to false.

What is the expected behavior?

I would suggest adding another config option; validateOnChange: bool validateOnBlur: bool

Other information

I’m considering abandoning my own attempt at a form library (it was a great learning experience though), since you certainly know more about the different pain points users have, and the internal workings of React and how to properly do performant libraries, considering your background with forms and React.

One thing I really liked with my solution was the ability to do sync validation onChange, and async (i.e network requests) on onBlur.

I haven’t figured out another way to do this just yet. But If we can have the library to validate on both onChange and onBlur this would be possible.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Dakkerscommented, Jan 11, 2020

A possibly related behaviour I quite like is having “onBlur” validation initially, and after the initial blur, validation is done onChange.

3reactions
czabajcommented, Apr 13, 2021

I encountered a very similar issue today and I think I found an almost ideal solution.

At first, the documentation for Field validate caught my attention - it passes field meta as a 3rd argument, thus we can detect in our field validator if we validate on blur or on change – the meta.active is true during onChange validation and false during onBlur. Note, that Form validatate receives only the values parameter, thus is unable to detect which validation we are in, but I favor Field validation in all cases as it is more declarative, simpler, and easier to maintain.

But now, how to trigger validation on the change as well as on blur? We could simply leave the Form validateOnBlur property untouched, thus our validate function will run on change only, then write some of our validators to skip validation, when the field is in meta.active and then rerun the validation onBlur.

Well, it turns out, triggering the validation is another nut to crack! Unfortunately, I did not found a public API for triggering a validation, but I dig into the source code and found the mighty runValidation function, which is not exported but is triggered by change, which we can control, so I created a simple mutator which, when called, “changes” the value of the specified field, to the same, unchanged value – it fakes “change” to just trigger validation.

And, with all these little components, we are able to run some validations on change and some validation on blur.

Check the CodeSandbox and note the beautiful validator composition, which runs my async validator onBlur only when all synchronous validator passes, whilst synchronous validators are run on every change. https://codesandbox.io/s/trigger-validation-onblur-mx7vx

NOTE: the example is forked from another example and a little bit complicated than necessary, but the important part shall be clear. The fundamental parts are triggerValidation mutator and an async field-level validation function that tests meta.active.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-hook-form: Validation not working when using onBlur ...
Validation will trigger on the blur event. Validation will trigger on the change event with each input, and lead to multiple re-renders.
Read more >
How onBlur and onChange events work in React
The changeHandler will be triggered only when you change something and focus out of the text input.
Read more >
The difference between onBlur vs onChange for React text ...
onBlur is fired when you have moved away from an object without necessarily having changed its value. onChange is only called when you...
Read more >
Validation - Formik
It will run after any onChange and onBlur by default. ... You can manually trigger both form-level and field-level validation with Formik using...
Read more >
Do not validate when typing, but on blur - Telerik UI for Blazor
It defines the event that triggers validation ( OnChange or OnInput ). ... Note that blurring the first input will always trigger validation,...
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