鍍金池/ 問(wèn)答/HTML/ element-ui中如何進(jìn)行異步驗(yàn)證整個(gè)表單

element-ui中如何進(jìn)行異步驗(yàn)證整個(gè)表單

場(chǎng)景:登錄
在點(diǎn)擊登錄按鈕時(shí)會(huì)將數(shù)據(jù)發(fā)送到后臺(tái),后臺(tái)會(huì)對(duì)數(shù)據(jù)進(jìn)行校驗(yàn),若尚末注冊(cè)或帳號(hào)密碼錯(cuò)誤時(shí)會(huì)返回給前端錯(cuò)誤信息,前端在拿到這個(gè)信息時(shí)如何使用element-ui或iview的驗(yàn)證功能將錯(cuò)誤信息展現(xiàn)在頁(yè)面中。

期望效果如下,錯(cuò)誤信息由UI框架的驗(yàn)證層渲染

clipboard.png


官網(wǎng)給出的異步示例不能符合項(xiàng)目實(shí)際需求,給出的示例是只要離開(kāi)焦點(diǎn)就進(jìn)行了對(duì)單個(gè)表單元素的驗(yàn)證

clipboard.png

注:不是使用Message組件來(lái)實(shí)現(xiàn)錯(cuò)誤提示!而是像第一張圖中郵箱和域名輸入框中沒(méi)輸入內(nèi)容時(shí)UI框架的驗(yàn)證層提示的錯(cuò)誤樣式

回答
編輯回答
話寡

1.前端也只能做一些模糊校驗(yàn)?zāi)?
2.你需要的異步校驗(yàn)我的理解是前端發(fā)送數(shù)據(jù)到后臺(tái),后臺(tái)校驗(yàn)是否存在賬號(hào),未存在提示用戶注冊(cè),已存在校驗(yàn)密碼是否正確,如果是這樣你在前端可以做下模糊校驗(yàn),像只能為數(shù)字和字母等
正則校驗(yàn)

let reg = /^[0-9a-zA-Z]+$/;
if(str&&!reg.test(str)){
 this.$message.warning('輸入不是數(shù)字或者字母!');}

3.發(fā)送數(shù)據(jù)到后臺(tái),后臺(tái)返回對(duì)應(yīng)的標(biāo)識(shí),前端通過(guò)this.$message.warning('輸入不是數(shù)字或者字母!');提示

2017年12月26日 07:29
編輯回答
柒槿年

async submit() {

    try {
  //向服務(wù)器發(fā)送數(shù)據(jù)
    } catch (err) {
      this.$message({
        message: err.response.data,
        showClose: true,
        type: 'error'
      })
    }
  }
  
  用element ui 的“Message 消息提示”組件
2018年7月28日 11:42
編輯回答
拼未來(lái)

給input標(biāo)簽添加一個(gè)失去焦點(diǎn)事件@blur設(shè)置一個(gè)布爾類(lèi)型的中間變量,當(dāng)失去焦點(diǎn)時(shí),置為false,當(dāng)中間變量為true時(shí)才提交,在點(diǎn)擊登錄時(shí)將中間變量設(shè)置為true

2017年7月22日 01:23
編輯回答
凹凸曼

覺(jué)得這一點(diǎn)vee-validate做得比較好,跟element-ui配合得還可以,其中data-vv-開(kāi)頭的為vee-validate的指令

    <el-form :model="loginForm" ref="loginForm" label-position="left" label-width="0px" class="login-container">
      <h3 class="title">JVUE-管理系統(tǒng)登錄</h3>
      <el-form-item prop="account" :error="errors.first('account')">
        <el-input type="text" v-model.trim="loginForm.account" placeholder="賬號(hào)" data-vv-name="account" v-validate
                  data-vv-rules="required||alpha_num||min:2" autofocus></el-input>
      </el-form-item>
      <el-form-item prop="password" :error="errors.first('password')">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" data-vv-name="password" v-validate
                  data-vv-rules="required" placeholder="密碼"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class="remember">自動(dòng)登錄</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:40%;" native-type="submit" @click.native.prevent="handleSubmit"
                   :loading="logining">登錄
        </el-button>
      </el-form-item>
    </el-form>
this.$validator.validateAll().then(result => {
        if (result) {
          this.logining = true
          let loginParams = {
            username: this.loginForm.account,
            password: this.loginForm.password,
            remember: this.checked ? 1 : 0
          }
          this.self.login(loginParams).then((res) => {
            this.logining = false
            this.$message({
              showClose: true,
              duration: 1500,
              message: '登錄成功'
            })
            // 初始化路由
            var path = this.$route.query.redirect
            this.$router.replace({path: path === '/' || path === undefined ? '/' : path})
          }).catch((err) => {
            this.logining = false
            this.$notify({
              title: '警告',
              message: err,
              type: 'warning',
              duration: 2500
            })
          })
        }
      }).catch(result => {
        this.$notify(messages.notifyCheckError())
      })
2018年7月27日 11:15