vee-validate 表單驗證

----好記性不如爛筆頭,掌握知識的最好方法莫過于把他記錄下來

vee-validate
vee-validate 一個輕量級的 vue表單驗證插件。

使用教程:

  • 1 安裝:
npm install vee-validate --save
  • 2 使用 中文提示(按照教程搗鼓了,不行,自己摸索的寫法。)
    main.js 添加
import Vue from 'vue'
import VeeValidate, {Validator}  from 'vee-validate';
import zh from 'vee-validate/dist/locale/zh_CN';

Validator.addLocale(zh);

const config = {
  locale: 'zh_CN'
};

Vue.use(VeeValidate,config);
  • 3使用
    第一個例子:
<div class="column is-12">
    <label class="label" for="email">Email</label>
    <p :class="{ 'control': true }">
        <input v-validate="'required|email'" :class="{'input': true, 'is-danger': errors.has('email') }" name="email" type="text" placeholder="Email">
        <span v-show="errors.has('email')" class="help is-danger">{{ errors.first('email') }}</span>
    </p>
</div>

簡化寫,只看有用代碼:

    <p>
        <input v-validate="'required|email'" name="email" type="text" placeholder="Email">
        <span v-show="errors.has('email')"  >{{ errors.first('email') }}</span>
    </p>

關(guān)于 提示:errors
==>errors.has('email')
==>errors.first('email')
這是什么玩意兒。莫急
直接輸出 {{errors}}就可以看到,驗證的時候返回的是個json數(shù)據(jù)

{
    "errors": [
        {
            "field": "email",
            "msg": " email 必須是有效的郵箱.",
            "rule": "email",
            "scope": "__global__"
        }
    ]
}

解釋如下:
errors.first('field') -- 獲取關(guān)于當(dāng)前field的第一個錯誤信息
collect('field') -- 獲取關(guān)于當(dāng)前field的所有錯誤信息(list)
has('field') -- 當(dāng)前filed是否有錯誤(true/false)
all() -- 當(dāng)前表單所有錯誤(list)
any() -- 當(dāng)前表單是否有任何錯誤(true/false)

tip:關(guān)于樣式,在項目中可以根據(jù)需求選擇,是使用自己的或者是現(xiàn)成的樣式庫(作者使用的是 Font Awesome)。

4 ok前方高能。最關(guān)心的莫過于自定義規(guī)則,或者修改規(guī)則
①:怎么修改默認(rèn)的屬性



這個是默認(rèn)的,怎么自定義。。。
就是將 email 字段改為 郵箱或者隨意字段。
方法一:

const dictionary = {
   zh_CN: {
    messages: {
      email: () => '這個郵箱有毒'
    },
     attributes:{
       email:'--郵箱--'
     }
  }
};
Validator.updateDictionary(dictionary);
提示語

為空時

方法二:
官方給出的自定義組件demo,前提是你定義了 custom-input

<div class="columns is-multiline">
    <div class="column is-12">
        <custom-input v-validate="'required|email'" data-vv-value-path="innerValue" data-vv-name="custom" label="Email" :has-error="errors.has('custom')">
        </custom-input>
        <span v-show="errors.has('custom')" class="help is-danger">{{ errors.first('custom') }}</span>
        <button @click="validate" type="button" class="button is-primary">Validate All</button>
    </div>
</div>

但是你可以忽略,有現(xiàn)成的input干嘛不用

    <div >
        <input v-validate="'required|email'" data-vv-name="丫" label="Email" :has-error="errors.has('丫')" />
        <span v-show="errors.has('丫')" class="help is-danger">{{ errors.first('丫') }}</span>
    </div>

ok,看效果



②增加自己的規(guī)則
添加手機號驗證

Validator.extend('phone', {
  messages: {
    zh_CN:field => field + '必須是11位手機號碼',
  },
  validate: value => {
    return value.length == 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
  }
});

<input v-validate="'required|phone'" name="phone" type="text" placeholder="Mobile">
<span v-show="errors.has('phone')" >{{ errors.first('phone') }}</span>

③當(dāng)使用 required 規(guī)則驗證時,輸入為空時提示:xxx是必須的,真是要瘋了。
如何修改:就一句話搞定

required:(field)=> "請輸入"+field

其實就是覆蓋他的規(guī)則,完整版:

const dictionary = {
  zh_CN: {
    messages:{
      //email: () => '這個郵箱有毒',
       ....
      required:(field)=> "請輸入"+field
    },
    attributes: {
     ......
    }
  }
};
Validator.updateDictionary(dictionary);

看效果:


④官方提供了好多現(xiàn)成的規(guī)則 Rules

使用方法,

v-validate="'required|max:6|email'"

實現(xiàn)功能:
①required:為空時提示
②max:最多可以輸入6個字符
③email:郵箱驗證


當(dāng)當(dāng)當(dāng)?,F(xiàn)在使用到的基本用法。
待完善。


推薦閱讀:

還是看官方文檔吧 雖然英語這硬傷
文檔:http://vee-validate.logaretm.com/localization.html
官方例子:http://vee-validate.logaretm.com/examples.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、簡介 Laravel 提供了多種方法來驗證應(yīng)用輸入數(shù)據(jù)。默認(rèn)情況下,Laravel 的控制器基類使用Valid...
    伊Summer閱讀 1,618評論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • HTML表單 在HTML中,表單是 ... 之間元素的集合,它們允許訪問者輸入文本、選擇選項、操作對象等等,然后將...
    蘭山小亭閱讀 3,507評論 2 14
  • Angular 支持非常強大的內(nèi)置表單驗證,maxlength、minlength、required 以及 pat...
    sunny_lvy閱讀 20,142評論 3 25
  • 22年12月更新:個人網(wǎng)站關(guān)停,如果仍舊對舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,412評論 22 257

友情鏈接更多精彩內(nèi)容