react-validation icon indicating copy to clipboard operation
react-validation copied to clipboard

Silent validation

Open abm0 opened this issue 7 years ago • 4 comments

Is there any ability to validate fields silently, without rendering error messages?

abm0 avatar Oct 10 '17 13:10 abm0

What's the purpose of doing it? You can always return null within rule component or just not to render error prop.

Lesha-spr avatar Oct 10 '17 13:10 Lesha-spr

Well, I currently need to render completely different component on the layout depending on form validity (render only if valid), and I have two cases, when form validation leads to undesirable effects:

  1. I need to check form validity on component mount, but I don't want error messages to be shown when user didn't do any actions with form;

  2. When user input value at certain form field, I need to check all other fields validity after every value update, what leads to rendering error messages for fields that user does not interact with.

abm0 avatar Oct 10 '17 14:10 abm0

For this case you can use HOC control which mixes in props isUsed and isChanged relative to was the control "blur"ed and "change"d and do your own rendering logic.

import { control } from 'react-validation';

@control
const Input = ({ error, isChanged, isUsed, ...props }) => (
  <div>
    <input {...props} />
    {isChanged && isUsed && error}
  </div>
);

Or without decoration:

import { control } from 'react-validation';

const Input = ({ error, isChanged, isUsed, ...props }) => (
  <div>
    <input {...props} />
    {isChanged && isUsed && error}
  </div>
);

export control(Input);

Lesha-spr avatar Oct 11 '17 09:10 Lesha-spr

Ok, thanks, I'll try it out!

abm0 avatar Oct 11 '17 12:10 abm0