search

Home  >  Q&A  >  body text

Why can't my element-ui-form validator be called?

code show as below:

**validateCode:[{validator:(rule, value, callback)=>{
    if (!value) { 
      callback(new Error('you have to input code')) 
    } else if(this.radioValue=="mobile"){
      validatingCo(this.user.mobile,value).then((res)=>{
        if(!res.code==20000){
           return callback(new Error('the code is wrong or expired'))
         } else{
             callback()
         }
      })
    }
    else{
       validatingCo(this.user.email,value).then((res)=>{
         if(!res.code==20000){
           return  callback(new Error('the code is wrong or expired'))
         } else{
           callback()
         }
       })
    }
  }, trigger:'blur'}]

Unfortunately, it doesn't execute and there are no errors. I want to know how to deal with it.

P粉274161593P粉274161593328 days ago419

reply all(1)I'll reply

  • P粉187677012

    P粉1876770122024-03-28 09:00:47

    I think the problem may be caused by this line:

    if(!res.code==20000){
      // Some stuff here
    }

    It should probably be:

    if(res.code!=20000){
      // Some stuff here
    }

    !res.code will always evaluate to true or false. So !res.code==20000 will always be false. No matter what is entered, the following error callbacks will not be executed:

    return callback(new Error('the code is wrong or expired'))

    Here is a small demonstration showing that "bar" will always be printed

    function simple_if(number) {
      if (!number == 20000) {
        return "foo"
      } else {
        return "bar"
      }
    }
    
    console.log(`res=20000. Expect 'bar': ${simple_if(2000)}`)
    console.log(`res=3. Expect 'foo': ${simple_if(3)}`)
    console.log(`res=0. Expect 'foo': ${simple_if(0)}`)

    reply
    0
  • Cancelreply