Veutify v-file-input validation
Asked Answered
H

2

5

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?

Hade answered 10/7, 2020 at 2:5 Comment(0)
H
10

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.

Hade answered 10/7, 2020 at 5:28 Comment(0)
W
0

You can get this behavior with only Vuetify's v-file-input by using the :rules prop - no need for VeeValidate.

Here is an example with the multiple option.

v-file-input with validation

<template>
   <v-file-input
     multiple
     :rules="[rules.requiredFile]"
   ></v-file-input>
</template>

<script setup lang="ts">

const rules = {
  requiredfile: (v: File[]) => {
    if (v.length == 0) {
      return "Must select at least one file.";
    } else {
      return true;
    }
  },
};

</script>
Wacker answered 25/3 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.