Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Validate Select Option With Vee-Validate

Going through vee-validates documentation I don't see anything for validating select inputs. So my question is can you validate selects? Currently what I try does not display an error message...

Here is the code

<select id="category" v-model="client.category" name="Category Type" v-validate="'required'">
  <option disabled>{{option}}</option>
  <option v-for="category in categories" :key="category.id" :value="category">{{ category }}</option>
 </select>
 <spanv-show="errors.has('Category Type')">{{ errors.first('Category Type') }}</span>
like image 599
TJ Weems Avatar asked Oct 28 '22 00:10

TJ Weems


1 Answers

Well I found this question and I know the answer would be the right on a year ago. But right now yes, we can validate an object in a select. So for those select using and object as a value you can add the required validation as a custom one like this:

extend('objectNotEmpty', {
  validate: (value) => {
    if (Object.keys(value).length > 0) {
      return true;
    }
    return i18n.t('GENERAL_VALIDATION_MESSAGES_REQUIRED');
  },
});

And of course add 'objectNotEmpty' in the list of the rules in your ValidationProvider

like image 166
pabloRN Avatar answered Nov 17 '22 07:11

pabloRN