7

Am using Vue Validate

i have the following in my vuevlidate

      validations: {
        user_form: {
            email: {required,email, isUnique(value) {
                    // standalone validator ideally should not assume a field is required
                    if (value === '') return true;
                    // simulate async call, fail for all logins with even length
                    return new Promise((resolve, reject) => {
                       this.$http.post("v1/user-management/users/email-registeredi",{email:value}).then((res)=>{
                            console.log("res is ", res);
                            resolve(true);
                        },(err)=>{
                            reject(false)
                        })
                    })
                }},
            role: {required},
            password: {required}
        }
    },

The above creates an endless loop of http requests especially when it gets an error

Where am i going wrong

Geoff
  • 6,277
  • 23
  • 87
  • 197
  • 2
    An endless loop of http requests implies that the `isUnique` function is called endlessly. I can't reproduce that with this [demo](https://codesandbox.io/s/4083p7r1o4). Can you edit the demo to reproduce the issue? – tony19 Sep 11 '18 at 00:46

2 Answers2

0

In case vue validate is not handling reject promise well and creating infinite loop.
You can try, async await for Vue validate's isUnique with try and catch returning false on error,
something like this.

validations: {
 user_form: {
  email: {
    required,
    email,
    async isUnique (value) {
        if (value === '') return true
        try{
          const response = await this.$http.post("v1/user-management/users/email-registeredi",{email:value})
          return true;
        }
        catch(e){
          return false;
        }   
      }
   }
 }
Helping hand
  • 2,800
  • 2
  • 19
  • 31
0

You don't need to use "new Promise" because vue-resource already do that. Try this:

validations: {
    user_form: {
        email: {required,email, isUnique(value) {
                // standalone validator ideally should not assume a field is required
                if (value === '') return true;
                // simulate async call, fail for all logins with even length

               return this.$http.post("v1/user-management/users/email-registeredi",{email:value}).then((res)=>{
                    console.log("res is ", res);
                    return true;
                },(err)=>{
                    return false;
                });
            }},
        role: {required},
        password: {required}
    }
},
Alim Giray Aytar
  • 1,274
  • 1
  • 13
  • 17