simple-react-validator
simple-react-validator copied to clipboard
How to create custom async validator ?
What do you mean by async validator? Like a validator that makes an ajax call or something to do validation that might take processing time? Please give some more info about what you are looking to do.
If that assumption is correct, we currently do not support that but I think that is a valid feature request. Am open to suggestions on how this works. Might have to do some debouncing or something to ensure it doesn't run too much like it does on render for the other validators. Maybe only when the .allValid
method is called or something.
Thank you @stuyam Yes Like a validator that makes an ajax call, I want to check if email is unique.
I have thought about this more, not sure if we are on the same page, I think you are suggesting that srv makes the ajax call and I think I was thinking that the ajax call it up to you but handled by a different validator method that calls async methods after regular validators so that the async validators are not run as much.
@stuyam this is my code
this.validator = new SimpleReactValidator({
validators: {
is_unique: {
message: 'The :attribute must be unique.',
rule: (val, params, validator) => {
UserService.uniqueEmail({email: val}).then(res => {
return res.data.length === 0
})
}
}
},
element: (message) =>
<div className="invalid-feedback"><span>{message}</span></div>
})
and in render :
{this.validator.message('email', this.state.user.email, 'required|email|is_unique:' + this.state.user.email)}