How do I dynamically set validations fields in vuelidate
Asked Answered
R

3

15

I'm using VueJS2 with vuelidate library. I can validate the fields based on the validation objects. The validation will execute during computed time. But My validations objects is fixed, instead of dynamic. I have some fields will hide based on the selection.

import { validationMixin } from 'vuelidate'
import { required, maxLength, email } from 'vuelidate/lib/validators'

export default {
mixins: [validationMixin],
validations: {
  company_name: { required },
  company_position_title: { required }
},
methods: {
  submit(){
    this.$v.touch();
    if(this.$v.$invalid == false){ 
      // All validation fields success
    }
  }
}
}

HTML

<v-select
  label="Who are you?"
  v-model="select" // can be 'company' or 'others'
  :items="items"
  :error-messages="selectErrors"
  @change="$v.select.$touch();resetInfoFields();"
  @blur="$v.select.$touch()"
  required
></v-select>

<v-text-field
  label="Company Name"
  v-model="company_name"
  :error-messages="companyNameErrors"
  :counter="150"
  @input="$v.companyName.$touch()"
  @blur="$v.companyName.$touch()"
  v-show="select == 'Company'"
></v-text-field>

<v-text-field
  label="Company Position Title"
  v-model="company_position_title"
  :error-messages="companyPositionErrors"
  :counter="150"
  @input="$v.companyPosition.$touch()"
  @blur="$v.companyPosition.$touch()"
  v-show="select == 'Company'"
></v-text-field>

<v-btn @click="submit">submit</v-btn>

Problem

When I select 'other' option and click submit, the this.$v.$invalid is still true. It should be false as there is no validation fields required. When I select 'company', that two fields must required and validated.

Retral answered 12/11, 2017 at 15:54 Comment(0)
F
16

you need a dynamic validation schema

validations () {
  if (!this.select === 'company') {
    return {
      company_name: { required },
      company_position_title: { required }
    }
  }
}

More info: Dynamic validation schema

Funest answered 4/12, 2017 at 14:56 Comment(2)
says its $dirty of undefinedRecoil
You cannot use an if statement inside an object declaration.Rogerson
C
7

Another way is using requiredIf

itemtocheck: {
  requiredIf: requiredIf(function () {
    return this.myitem !== 'somevalue'
  }),
  minLength: minLength(2) },
Chestonchest answered 20/9, 2018 at 22:10 Comment(1)
minLength will still be applied, so it does not workFechter
B
2

Here's how it's done with Vue3 + composition API according to the docu.

const state = reactive({
  shippingAddress: '',
  billingSameAsShipping: false,
  billingAddress: ''
})
const rules = computed(() => {
  const localRules = {
    shippingAddress: { required }
  }
  if (!state.billingSameAsShipping) {
    // if billing is not the same as shipping, require it
    localRules.billingAddress = {
      required
    }
  }
  return localRules
})
const v$ = useVuelidate(rules, state)
Bunt answered 3/3, 2023 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.