Vuetify如何在所有验证规则都正确之前禁用按钮?

时间:2019-09-04 17:26:48

标签: vue.js vuetify.js

我正在使用Vuetify创建一个简单的表单,该表单中的数据正在通过与每个字段相关的一些规则进行验证。如果这些验证规则中的任何一个失败,我想做的就是禁用“提交”按钮。我该怎么办?我想了解的是如何检查每个规则的状态,以便可以绑定按钮上的disabled属性。

<template>
  <v-card>
    <v-card-text>
      <v-combobox
        v-model="addTool.name"
        :rules="rules.name"
        :items="toolNames"
        counter
        label="Name of the tool"
      ></v-combobox>
      <v-combobox
        multiple
        :small-chips="true"
        :deletable-chips="true"
        :hide-selected="true"
        :clearable="true"
        v-model="addTool.categories"
        :rules="rules.categories"
        label="Select/Add categories"
        :items="this.toolCategories"
        :search-input.sync="searchCat"
        @change="searchCat = ''"
      ></v-combobox>
      <v-text-field
        v-model="addTool.site"
        label="URL for the tool. It is best to use a Github repo link."
        :rules="rules.site"
      ></v-text-field>
    </v-card-text>
    <v-card-actions>
      <v-spacer></v-spacer>
      <v-btn @click="submit" color="grey darken-4" class="green--text text--accent-2" dark>Add</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
export default {
  data() {
    return {
      dialog: false,
      searchCat: "",
      addToolMessage: undefined,
      addTool: {
        name: "",
        categories: [],
        created_on: "",
        site: ""
      },
      rules: {
        name: [
          v => !!v || "Required.",
          v => v.length < 80 || "Name is too long",
          v =>
            !this.toolsData
              .map(n => n.name.toLowerCase())
              .includes(v.toLowerCase()) || "This tool already exists"
        ],
        categories: [v => v.length != 0 || "At least one category is needed"],
        site: [
          v => !!v || "Required.",
          v =>
            new RegExp(
              "^(?:(?:https?|ftp)://)(?:S+(?::S*)?@)?(?:(?!(?:10|127)(?:.d{1,3}){3})(?!(?:169.254|192.168)(?:.d{1,3}){2})(?!172.(?:1[6-9]|2d|3[0-1])(?:.d{1,3}){2})(?:[1-9]d?|1dd|2[01]d|22[0-3])(?:.(?:1?d{1,2}|2[0-4]d|25[0-5])){2}(?:.(?:[1-9]d?|1dd|2[0-4]d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:.(?:[a-z\u00a1-\uffff]{2,})).?)(?::d{2,5})?(?:[/?#]S*)?$",
              "ig"
            ).test(v) || "Not a valid URL",
          v =>
            !this.toolsData.map(u => u.site).includes(v) ||
            "This tool already exists"
        ]
      }
    };
  }
};
</script>

2 个答案:

答案 0 :(得分:2)

如果将输入元素包装在v-form中,然后将v-model附加到form上,Vuetify将跟踪表单的有效性:

<v-form v-model="isFormValid">
  <!-- all input elements go here -->
</v-form>

<!-- disable if form is not valid -->
<v-btn :disabled="!isFormValid">Add</v-btn>
data: () => ({
  isFormValid: false, // also add isFormValid to component's data 
})

您可以在此处了解更多信息:https://vuetifyjs.com/en/components/forms

答案 1 :(得分:0)

您可以为此创建一个计算属性。假设namesite是必需的,所以:

<temlate>
  <v-btn :disabled="isAddButtonDisabled" @click="submit" color="grey darken-4" class="green--text text--accent-2" dark>Add</v-btn>
</template>
<script>
export default {
  data() {
    return {
      addTool: {
        name: "",
        categories: [],
        created_on: "",
        site: ""
      },
    };
  },
  computed: {
    isAddButtonDisabled() {
      return !(this.addTool.name || this.addTool.site);
      // means u have neither name nor site  
    },
  },
};
</script>

如果需要检查表单是否有效,可以执行以下操作:

export default {
  computed: {
    formValid () {
      // loop over all contents of the fields object and check if they exist and valid.
      return Object.keys(this.addTool).every(field => {
        return this.addTool[field] && this.addTool[field].valid;
      });
    }
  }
}

从这里:https://github.com/logaretm/vee-validate/issues/853