我正在使用Vuejs2和vee-validate进行表单验证。这是一个很棒的程序包,但是我正在努力实现一个有条件的必填字段。
选择特定的单选选项时,我希望有两个选择字段。当未选择该广播时,我希望两个选择字段为可选。
我尝试使用attachdetach方法。我可以成功分离验证。而且我可以看到附加字段时该字段显示在fields对象中。但是验证者没有选择它。
这是我的代码:

<template>
  <form class="ui form" role="form" method="POST" action="/activate" v-on:submit.prevent="onSubmit" :class="{ 'error': errors.any() }">
    <div class="ui segment">
      <h4 class="ui header">Basic Company Information</h4>
      <div class="ui message">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
      <div class="field" :class="{ 'error': errors.has('name') }">
        <div class="ui labeled input">
          <label class="ui label" for="name">
            Company
          </label>
          <input id="name" type="text" name="name" v-validate="'required'" v-model="name">
        </div>
      </div>
      <div class="ui error message" v-show="errors.has('name')">
        <p>{{ errors.first('name') }}</p>
      </div>
      <div class="grouped fields" :class="{ 'error': errors.has('organisation_type_id') }">
        <label for="organisation_type_id">Organisation type</label>
        <div class="field">
          <div class="ui radio checkbox">
            <input class="hidden" type="radio" name="organisation_type_id" value="1" data-vv-as="organisation type" v-validate="'required'" v-model="organisation_type">
            <label>Buyer</label>
          </div>
        </div>
        <div class="field">
          <div class="ui radio checkbox">
            <input class="hidden" type="radio" name="organisation_type_id" value="2" checked>
            <label>Seller</label>
          </div>
        </div>
      </div>
      <div class="ui error message" v-show="errors.has('organisation_type_id')">
        <p>{{ errors.first('organisation_type_id') }}</p>
      </div>
      <div v-show="organisation_type == '2'">
        <div class="field" :class="{ 'error': errors.has('countries[]') }">
          <label for="countries">Countries</label>
          <select class="ui fluid search dropdown" id="countries" name="countries[]" multiple data-vv-as="countries" v-validate="'required'">
            <option v-for="country in countries" :value="country.value">{{ country.text }}</option>
          </select>
        </div>
        <div class="ui error message" v-show="errors.has('countries[]')">
          <p>{{ errors.first('countries[]') }}</p>
        </div>
        <div class="ui message field-description">
          <p>Select all the countries you export to.</p>
        </div>
        <div class="field" :class="{ 'error': errors.has('ciphers[]') }">
          <label for="ciphers">Ciphers</label>
          <select class="ui fluid search dropdown" id="ciphers" name="ciphers[]" multiple data-vv-as="ciphers" v-validate="'required'">
            <option v-for="cipher in ciphers" :value="cipher.value">{{ cipher.text }}</option>
          </select>
        </div>
        <div class="ui error message" v-show="errors.has('ciphers[]')">
          <p>{{ errors.first('ciphers[]') }}</p>
        </div>
        <div class="ui message field-description">
          <p>Select all the ciphers you support.</p>
        </div>
      </div> <!-- End organisation_type_id -->
      <button class="ui fluid green button" type="submit">Continue</button>
    </div> <!-- .ui.segment -->
  </form>
</template>

<script>
  export default {
    props: ['countriesJson', 'ciphersJson'],
    data() {
      return {
        name: null,
        organisation_type: '2',
        countries: [],
        ciphers: [],
      }
    },
    watch: {
      organisation_type: function(value) {
        var vm = this
        if (value == '2') {
          vm.$validator.attach('countries[]', 'required');
          const select = document.getElementById('countries');
          select.addEventListener('change', function() {
            vm.$validator.validate('required', this.value);
          });
          vm.$validator.attach('ciphers[]', 'required');
          const select = document.getElementById('ciphers');
          select.addEventListener('change', function() {
            vm.$validator.validate('required', this.value);
          });
        } else {
          vm.$validator.detach('countries[]')
          vm.$validator.detach('ciphers[]')
        }
      },
    },
    mounted() {
      this.countries = JSON.parse(this.countriesJson)
      this.ciphers = JSON.parse(this.ciphersJson)
    },
    methods: {
      onSubmit: function(e) {
        this.$validator.validateAll().then(success => {
          e.target.submit()
        }).catch(() => {
          return
        })
      }
    }
  }
</script>

最佳答案

可能您是说像这样吗?

<input id="name"
       type="text"
       name="name"
       v-validate="{ required: this.isRequired }"
       v-model="name">
其中“isRequired”是计算字段,取决于条件

关于validation - vee-validate : Required only if a condition is met,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43024684/

10-13 01:43