simple-react-validator icon indicating copy to clipboard operation
simple-react-validator copied to clipboard

How to create custom async validator ?

Open RiadhRahmi opened this issue 5 years ago • 4 comments

RiadhRahmi avatar Dec 21 '18 23:12 RiadhRahmi

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.

stuyam avatar Dec 27 '18 23:12 stuyam

Thank you @stuyam Yes Like a validator that makes an ajax call, I want to check if email is unique.

RiadhRahmi avatar Dec 28 '18 09:12 RiadhRahmi

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 avatar Jan 29 '19 18:01 stuyam

@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)}

RiadhRahmi avatar Jan 30 '19 11:01 RiadhRahmi