react-validation
react-validation copied to clipboard
Silent validation
Is there any ability to validate fields silently, without rendering error messages?
What's the purpose of doing it? You can always return null
within rule component or just not to render error
prop.
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:
-
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;
-
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.
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);
Ok, thanks, I'll try it out!