Vue / Vuetify - 如何对每个芯片项目而不是整个 select 输入进行验证

Vue / Vuetify - How to make a validation on each chip item instead of the entire select input

有谁知道如何对每个芯片项目进行验证(使用 vee-validate)?

我有这段代码:

<v-select
  class="elevation-0 mt-border-bottom"
  v-model="PhoneNumber"
  label="Add phone number"
  chips
  tags
  solo
  prepend-icon="phone"
  clearable
  :error-messages="errors.collect('Phone Number')"
  v-validate="'required|numeric'"
  data-vv-name="Phone Number"
  required
>
    <template slot="selection" slot-scope="data">
        <v-chip
          close
          outline
          dark
          @input="remove(data.item)"
          :selected="data.selected"
        >
            <strong>{{ data.item }}</strong>
        </v-chip>
    </template>
</v-select>

<script>
    export default {
        data () {
            return {
                PhoneNumber: []
            }
        },
        methods: {
            async submitNewNumber () {
                await this.$validator.validateAll().then((isValid) => {
                    if (isValid) {
                        console.log('submitted')
                    } else {
                        return false
                    }
                })
            }
        }
    }
</script>

现在验证仅在整个 Phone 数字输入上进行。 我想知道如何让它在每个芯片上工作,在此输入中将 min_value 设置为 9,将 max_value 设置为 15。

Vuetify - 芯片使用:https://vuetifyjs.com/en/components/chips

Vuetify - Vee 验证:https://vuetifyjs.com/en/components/forms#example-vee-validate

Vee-validate - 验证规则:https://baianat.github.io/vee-validate/guide/rules.html

谢谢

v-chip 似乎没有内置验证功能。所以我使用默认验证(不是 vee-validate)。这样你就可以看到 v-select 的结果。然后您可以遍历结果并验证每个值。

        inputRules = [
            (v: any) => {
                if (!v || v.length < 1)
                    return 'Input is required';
                else if (v.length > 0) {
                for (let i = 0; i < v.length; i++) {
                    if (v[i].length > 9)
                        return 'Invalid Number';
                }
            }
                else return true;
            }
        ];

<v-form ref="form" v-model="valid" lazy-validation>
        <v-select
            class="elevation-0 mt-border-bottom"
            v-model="phoneNumber"
            label="Add phone number"
            chips
            tags
            solo
            prepend-icon="phone"
            clearable
            data-vv-name="Phone Number"
            required
            :rules="inputRules"
        >
            <template slot="selection" slot-scope="data">
                <v-chip
                    close
                    outline
                    dark
                    @input="remove(data.item)"
                    :selected="data.selected"
                >
                    <strong>{{ data.item }}</strong>
                </v-chip>
            </template>
        </v-select>
        <v-btn @click.native="submitNewNumber">Test</v-btn>
    </v-form>

为此,我们必须

  1. 将值验证为数组(因此单独检查每个项目)
  2. 为自己定义 vee-validation 规则
// ES6 code
import {extend} from "vee-validate"
import {isEmail, isArray} from "validator"

// e.g. check each chip to contain valid email
extend("chipUrlRule", function (val) {
  if (!isArray(val) || val.length < 0) {
    return "Enter at least one Email"
  }

  for (let i = 0; i < val.length; i++) {
    if (!isEmail(val[i])) {
      return `${val[i]} is not a valid Email Address`
    }
  }

  return true

})