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

Custom validation rule with a promise #90

Closed
menaka123 opened this issue May 28, 2019 · 3 comments
Closed

Custom validation rule with a promise #90

menaka123 opened this issue May 28, 2019 · 3 comments
Assignees

Comments

@menaka123
Copy link

I am trying to verify by getting validation from back end, I have added a rule

this.validator = new SimpleReactValidator({
            element: message => message,
            validators: {
                hasAHouseNumber: {
                    message: 'The :attribute must contain a number.',
                    rule: function(val, params, validator) {
                        API.getInstance().getPlaceDetails(val)
                            .then(response => {
                                if (response.status === 200 && response.data.status === 'OK')
                                {
                                    console.log(response.data.result.address_components)
                                    response.data.result.address_components.some(item => {
                                        if (item.types.indexOf('street_number') > -1) {
                                            console.log('here')
                                            return true
                                        }
                                    })
                                }
                                return false
                            }).catch(error => {
                                console.log(error)
                                return false
                            })
                        // return false
                    }
                }
            }
        });

But validation rules do not seem to get the result from the promise, am I doing something wrong here ?

@stuyam stuyam self-assigned this May 30, 2019
@ej2015
Copy link
Contributor

ej2015 commented May 30, 2019

Is it because your rule is async, so the validation returns before it resolves? You may want to try async/await to force resolving the promise.

@stuyam
Copy link
Collaborator

stuyam commented May 31, 2019

Yeah right now the that method needs to immediately return true or false, Im not sure how an async validator would work.

I had worked on a solution for it at one point but it never was completed. But basically the idea was to be able to have validators that you can define as async and will be called in order on "submit" so you don't make tons of ajax calls as you type. Still open to suggestions about how to accomplish this.

@stuyam
Copy link
Collaborator

stuyam commented May 31, 2019

Attaching to #40 and closing this issue.

@stuyam stuyam closed this as completed May 31, 2019
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

3 participants