Search code examples
vue.jsvuetify.jsvee-validate

Veutify v-file-input validation


I would like to validate if a file has been chosen using v-file-input and ValidationProvider from vee-validate.

Below is my code:

<v-flex>
  <ValidationProvider rules="required" v-slot="{ errors }">
    <v-file-input
      show-size
      accept=".xlsx"
      placeholder="Click here to select your file"
      label="File name"
      :error="errors.length > 0"
      :error-messages="errors[0]"
      @change="selectFile"
    >
    </v-file-input>
  </ValidationProvider>
</v-flex>

Somehow the validation works but it works too well even after I have chosen a file:

enter image description here

I am not sure what I have done wrong?


Solution

  • Found out that I have to do it this way, not sure why mine above not working:

    rules: [
      v => !!v || 'File is required',
      v => (v && v.size > 0) || 'File is required',
    ]
    

    And my form:

    <v-flex>
      <ValidationProvider :rules="rules" v-slot="{ errors }">
        <v-file-input
          show-size
          accept=".xlsx"
          placeholder="Click here to select your file"
          label="File name"
          :error="errors.length > 0"
          :error-messages="errors[0]"
          @change="selectFile"
        >
        </v-file-input>
      </ValidationProvider>
    </v-flex>
    

    https://codepen.io/subashdbc/pen/eYpVOKq

    Posting the codes here to help anyone who needs this.