Laravel 5.6.7 和 Vue.js 中的自定义错误消息(特定组件)

Custom Error Message in Laravel 5.6.7 and Vue.js in particular Component

我正在使用 Laravel 5.6.7 和 vue.js

验证消息

当验证在 vue.js 中运行时,它表示 "The User Name field is required."。可以是"Please enter user name"吗?我可以为同一字段使用两个自定义错误消息吗?例子

  1. Required: Please enter username.
  2. Alpha: Please enter alpha chars only.

我正在使用 vee-validate 包进行表单验证

下面是blade中的代码

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <create-user></create-user>
        </div>
    </div>
</div>

vue.js 组件中的代码。

<template>
    <form role="form" class="row">      
        <input name="User Name" v-validate data-vv-rules="required|alpha" type="text"   
             v-model="createForm.UserName">
        <p v-if="errors.has('User Name')">{{ errors.first('User Name') }}</p>           
        <button type="button" @click="validateBeforeSubmit()" class="btn btn-primary">
            Create
        </button>
    </form>
</template>

<script>
    export default {
        data() {
            return {
                createForm: {  
                    UserName: ''
                }
            };
        },        
        methods: {
            validateBeforeSubmit() {
                this.$validator.validateAll();
            }
        }
    }
</script>

那是行不通的 使用 ajax 获取数据 在 vuejs 和服务器之间

要实现您想要的效果,您可以使用 Field Specific Custom Messages

没什么,您只需创建一个 customMessages 字典并使用 .localize().

将其添加到您的验证器

因此,您的模板中没有任何变化,但在您的 <script> 中,您将声明 customMessages 并将其应用到 created() 生命周期挂钩中,如下面和演示中所示:

<script>
    const customMessages = {                                              // added
        custom: {                                                         // added
            'User Name': {                                                // added
                required: 'Required: Please enter username',              // added
                alpha: 'Alpha: Please enter alpha chars only.'            // added
            }                                                             // added
        }                                                                 // added
    };                                                                    // added

    export default {
        data() {
            return {
                createForm: {  
                    UserName: ''
                }
            };
        },
        created() {                                                       // added
            // change 'en' to your locale                                 // added
            this.$validator.localize('en', customMessages);               // added
        },                                                                // added
        methods: {
            validateBeforeSubmit() {
                this.$validator.validateAll();
            }
        }
    }
</script>

可运行演示:

Vue.use(VeeValidate);

const customMessages = {
  custom: {
    'User Name': {
      required: 'Required: Please enter username',
      alpha: 'Alpha: Please enter alpha chars only.'
    }
  }
};

Vue.component('create-user', {
  template: '#create-user-template',
  data() {
    return {
      createForm: {
        UserName: ''
      }
    };
  },
  created() {
    // change 'en' to your locale
    this.$validator.localize('en', customMessages);
  },
  methods: {
    validateBeforeSubmit() {
      this.$validator.validateAll();
    }
  }
});

new Vue({
  el: '#app'
})
<script src="https://unpkg.com/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>

<template id="create-user-template">
   <form role="form" class="row">      
        <input name="User Name" v-validate data-vv-rules="required|alpha" type="text"   
             v-model="createForm.UserName">
        <p v-if="errors.has('User Name')">{{ errors.first('User Name') }}</p>           
        <button type="button" @click="validateBeforeSubmit()" class="btn btn-primary">
            Create
        </button>
    </form>
</template>

<div id="app">
  To see the 'required' message: type "x", then delete<br>
  To see the 'alpha' message: type "_"<br><br>
  <div class="container-fluid">
      <div class="row justify-content-center">
          <div class="col-md-12">
              <create-user></create-user>
          </div>
      </div>
  </div>
</div>