Cannot validate fields in vue.js

Viewed 452

I have dynamic fields generated on my form, All the validations that are to be set are retrieved from database (i.e. whether the field is required or optional and numeric value or string are all saved in database), Now when the page is rendered i want to validate my form i'm trying to write my own custom methods but it cannot validate the form,the issue is in custom method error message is not shown and the form is also submitting without validation.

<input v-model="field.user_value" :name="field.name" v-on:input="validateTextInput(field.field_validations,$event)" type="text" class="form-control input-md form-control"/>

methods: {
    validateTextInput(validations,event)
    {
      if(validations.req=='required')
      {
       if(event.target.value=='')
       {
         alert('This field is required');
         return false;
       }

       if(validations.allowed=='string')
       {
        
       }


      }
1 Answers

You're not using the correct variable for the event, use $event instead of e.target.value

<input v-model="field.user_value" :name="field.name" v-on:input="validateTextInput(field.field_validations, $event)" type="text" class="form-control input-md form-control"/>

methods: {
    validateTextInput(validations,event)
    {
      if(validations.req=='required')
      {
       if(event.target.value=='')
       {
         alert('This field is required');
         return false;
       }

       if(validations.allowed=='string')
       {
        
       }


      }
Related