Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create custom async validator ? #40

Open
RiadhRahmi opened this issue Dec 21, 2018 · 4 comments
Open

How to create custom async validator ? #40

RiadhRahmi opened this issue Dec 21, 2018 · 4 comments
Assignees

Comments

@RiadhRahmi
Copy link

No description provided.

@stuyam
Copy link
Collaborator

stuyam commented Dec 27, 2018

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.

@RiadhRahmi
Copy link
Author

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

@stuyam stuyam self-assigned this Jan 22, 2019
@stuyam
Copy link
Collaborator

stuyam commented Jan 29, 2019

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.

@RiadhRahmi
Copy link
Author

RiadhRahmi commented Jan 30, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants