1
votes

I am using Laravel 5.6.7 and vue.js

Validation Message

When the validation works in vue.js, it says "The User Name field is required.". Can it be "Please enter user name"? Can I use two custom error messages for same field? Example

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

I am using vee-validate Package for Form Validation

Below is the code in blade

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

Code in vue.js Component.

<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>
2

2 Answers

4
votes

To achieve what you want you can use Field Specific Custom Messages.

Not much to it, you just create a customMessages dictionary and add it to your validator using .localize().

So, nothing in your template changes, but in your <script> you would declare the customMessages and apply it in the created() lifecycle hook, as shown below and in the demo:

<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>

Runnable Demo:

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>
-3
votes

that is will not work use ajax to get data between vuejs and server