---
# System prepended metadata

title: Vee-Validation 表單驗證套件 (5-9)
tags: [5. 進階Api, Vue]

---

# Vee-Validation 表單驗證套件 (5-9)
###### tags: `Vue`、`5. 進階Api`
2022.3.8

參考筆記：https://hackmd.io/FFv0a5cBToOATP7uI5COMQ

### 筆記
1. **引用套件前置動作**
    * **遷入套件**
    ```
    <!-- VeeValidation 主套件 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vee-validate/4.5.8/vee-validate.min.js"></script>
    <!--- VeeValidation規則 ---->
    <script src="https://cdn.jsdelivr.net/npm/@vee-validate/rules@4.5.8/dist/vee-validate-rules.min.js"></script>
    <!-- VeeValidation多國語系 -->
    <script src="https://cdn.jsdelivr.net/npm/@vee-validate/rules@4.5.8/dist/vee-validate-rules.min.js"></script>
    ```
    * **加入特定規則**（email：姓箱格式驗證、required：輸入欄不能為空)
    &emsp;&emsp;**Vee-Validata全部規則可以參考:[連結](https://vee-validate.logaretm.com/v4/guide/global-validators#vee-validaterules)**
    
    ```
    VeeValidate.defineRule('email', VeeValidateRules['email']);
    VeeValidate.defineRule('required', VeeValidateRules['required']);
    ```
    * **讀取特定資源**(加入多國語系)
    &emsp;&emsp;**需先將[外部資源](https://github.com/logaretm/vee-validate/blob/vee-validate%404.1.16/packages/i18n/src/locale/zh_TW.json)儲存至本地**
    ```
    VeeValidateI18n.loadLocaleFromURL('./zh_TW.json');
    
    VeeValidate.configure({
         generateMessage: VeeValidateI18n.localize('zh_TW'),
         validateOnInput: true,
    });
    ```
    * **鑲嵌元件**(使用全域宣告)
    ```
    app.component('VForm', VeeValidate.Form);
    app.component('VField', VeeValidate.Field);
    app.component('ErrorMessage', VeeValidate.ErrorMessage);
    ```
2. **使用遷入元件，取代原本標籤**
   * **元件標籤**
     **<v-form>**：取代form表單標籤。
    **<v-field>**：取代所有輸入框。
     **<error-message>**：顯示錯誤訊息。
    
    * **特殊注意事項**
       &emsp;&emsp;<v-form>中，v-slot是將元件插巢於此，{{ errors }} 存放錯誤訊息表單。
    &emsp;&emsp;<v-field>與<error-message>中，name="名稱"
要相對應。
    &emsp;&emsp;<v-field> 中 :class="{'is-invalid':errors['name']}"要與<error-message> class="invalid-feedback"呼應。

### 完整實作
```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<!-- boostrap & Vue 套件 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vue@3"></script>
<!------------------------->

<!-------------------- VeeValidation 驗證套件 ------------------------->
<!-- VeeValidation 主套件 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vee-validate/4.5.8/vee-validate.min.js"></script> 
<!--- VeeValidation規則 ---->
<script src="https://cdn.jsdelivr.net/npm/@vee-validate/rules@4.5.8/dist/vee-validate-rules.min.js"></script>
<!-- VeeValidation多國語系 -->
<script src="https://cdn.jsdelivr.net/npm/@vee-validate/i18n@4.5.8/dist/vee-validate-i18n.min.js"></script>
<!--------------------------------------------------------------------->

<body>
    <div class="container main my-5"><div class="row">
        <div id="app">
            <h2>套用一個現成的流程</h2>
            <!-- <form> -->
                <v-form @submit="onSubmit" v-slot="{ errors }">
                    <div class="mb-3">
                        <label for="email" class="form-label">Email</label>
                        <v-field type="email" name="email" class="form-control" :class="{'is-invalid':errors['email']}" placeholder="請輸入Email" rules="email|required" v-model="user.email"></v-field>
                        <error-message name="email" class="invalid-feedback"></error-message>
                        <!-- <input type="email" class="form-control" placeholder="請輸入Email"> -->
                        <!-- <span class="invalid-feedback"></span> -->
                    </div>

                    <div class="mb-3">
                        <label for="name" class="form-label">姓名</label>
                        <v-field type="text" name="name" class="form-control" :class="{'is-invalid':errors['name']}" placeholder="請輸入姓名" rules="required" v-model="user.name"></v-field>
                        <error-message name="name" class="invalid-feedback"></error-message>
                        <!-- <input type="text" class="form-control" placeholder="請輸入姓名"> -->
                        <!-- <span class="invalid-feedback"></span> -->
                    </div>

                    <div class="mb-3">
                        <label for="phone" class="form-label">Phone</label>
                        <v-field type="text" name="phone" class="form-control" :class="{'is-invalid':errors['phone']}" placeholder="請輸入電話" :rules="isPhone" v-model="user.phone"></v-field>
                        <error-message name="phone" class="invalid-feedback"></error-message>
                        <!-- <input type="text" class="form-control" placeholder="請輸入電話"> -->
                        <!-- <span class="invalid-feedback"></span> -->
                    </div>

                    <div class="mb-3">
                        <label for="region" class="form-label">地區</label>
                        <v-field as="select" name="region" class="form-control" :class="{'is-invalid':errors['region']}" :rules="isRegion" v-model="user.region">
                            <option value="">請選擇地區</option>
                            <option value="台北市">台北市</option>
                            <option value="高雄市">高雄市</option>
                        </v-field>
                        <error-message name="region" class="invalid-feedback"></error-message>
                        <!-- <select name="地區" id="region" class="form-control">
                            <option value="">請選擇地區</option>
                            <option value="台北市">台北市</option>
                            <option value="高雄市">高雄市</option>
                        </select> -->
                        <!-- <span class="invalid-feedback"></span> -->
                    </div>
                    <div class="mb-3">
                        <label for="address" class="form-label">地址</label>
                        <v-field type="text" name="address" class="form-control" :class="{'is-invalid':errors['address']}" placeholder="請輸入地址" rules="required" v-model="user.address"></v-field>
                        <error-message name="address" class="invalid-feedback"></error-message>
                        <!-- <input type="text" class="form-control" placeholder="請輸入地址"> -->
                        <!-- <span class="invalid-feedback"></span> -->
                    </div>
                    <button class="btn btn-primary" type="submit">Submit</button>
                </v-form>
            <!-- <form> -->
        </div>
    </div></div>

    <script type="module">
        console.log(VeeValidate);
        // ---------- 加入特定規則 ----------
        VeeValidate.defineRule('email', VeeValidateRules['email']);
        VeeValidate.defineRule('required', VeeValidateRules['required'])
        // ---------------------------------


        // --------- 讀取外部的資源 ----------
            //---------加入中文語系--------
            VeeValidateI18n.loadLocaleFromURL('./zh_TW.json');
              // Activate the locale
            VeeValidate.configure({  
                generateMessage: VeeValidateI18n.localize('zh_TW'),  //切換成中文版
                validateOnInput: true, // 調整為：輸入文字時，就立即進行驗證
                });
              // ---------------------------
        // ----------------------------------


        const app = Vue.createApp({
            data(){
                return{
                    user:{
                        email:'',
                        name:'',
                        region:'',
                        address:'',
                        phone:''
                    }
                }                
            },
            methods:{
                onSubmit(){
                    console.log(this.user)
                },
                isPhone(value){
                    const phoneNumber = /^(09)[0-9]{8}$/
                    return phoneNumber.test(value) ? true:'需要正確的電話號碼'
                },
                isRegion(value){
                    if(value!=""){
                        return true
                    }
                    return '請選擇地區'
                }

            },
        });

        //------------ 加入VeeValidate 中表單元件 ------------
        app.component('VForm', VeeValidate.Form);
        app.component('VField', VeeValidate.Field);
        app.component('ErrorMessage', VeeValidate.ErrorMessage);
        //---------------------------------------------------
        
        app.mount('#app');
    </script>

</body>
</html>
```