我使用Vuejs2和vee validate进行表单验证.这是一个很棒的包,但是我很难实现一个有条件的必填字段.

当 Select 一个特定的单选选项时,我希望需要两个select字段.如果没有 Select radio ,我希望这两个 Select 字段是可选的.

我试过使用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"是计算字段,取决于条件

Vue.js相关问答推荐

VueJS使所有组件崩溃,而不是只有一个

Nuxt 3 I18N浏览器检测不起作用

如何使用composition api v-model Select 框获取其他JSON 值?

在 Vuejs 中更新 Chartjs 图表数据集

vue 不重新渲染数据更改

如何在 vue2.7 中删除 slot-scope

Nuxt3-Vue中的useRoute和useRouter有什么区别

TailwindCSS 为什么前者比后者重要?

在组件之间共享 websocket 连接

理解 VueJS 中的组件 props

用 axios 搜索:新字符时取消之前的请求

Express.js + lint 出错

Vuetify v-select 组件宽度变化

SVG 加载 vue-svg-loader; [Vue 警告]:无效的组件定义

在 Vue.js 中,为什么我们必须在导入组件后导出它们?

如何在 Vue.js 中获取组件元素的 offsetHeight?

动态注册本地 Vue.js 组件

如何在 Vuex 中获取 Vue 路由参数?

使用 axios 和 vue.js 取消先前的请求

Vuex Mutation 正在运行,但组件在 vue 开发工具中手动提交之前不会更新