Meena Chaudhary
Meena Chaudhary

Reputation: 10665

Vuetify v-combobox validations not working

I have a Vuetify(v. 1.9.0) v-combobox with validation that at least one item should be selected from the menu. For some reason the validation is not triggered and there are no errors in the console.

<v-combobox
  multiple
  item-text="name"
  item-value="id"
  :items="items"
  :rules="[rules.required]"
></v-combobox>

<script>
  export default {
     data: () => ({
       rules: {
         required: value =>
           !!value || "Required."
       }
     })
  }
</script>

What am I missing?

Upvotes: 2

Views: 7419

Answers (1)

Gabriel Willemann
Gabriel Willemann

Reputation: 1921

Try this example:

<template>
  <v-app>
    <v-content>
      <v-form ref="form">
        <div>
          <v-combobox
            multiple
            item-text="name"
            item-value="id"
            :items="items"
            :rules="[required]"
            v-model="selected"
          />
          selected = {{ selected }}
        </div>
        <div>
          <v-btn @click="$refs.form.validate()">Validate</v-btn>
        </div>
      </v-form>
    </v-content>
  </v-app>
</template>

<script>
export default {
  name: 'app',
  data: () => ({
    selected: null,
    items: [
      { id: 1, name: 'Option 1' },
      { id: 2, name: 'Option 2' },
      { id: 3, name: 'Option 3' },
    ],
  }),
  methods: {
    required(value) {
      if (value instanceof Array && value.length == 0) {
        return 'Required.';
      }
      return !!value || 'Required.';
    },
  },
};
</script>

Upvotes: 12

Related Questions