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

孤者浪人 提交于 2020-12-31 05:44:38

问题


Does anyone know how to make validation (with vee-validate) on each chips item?

I have this piece of code:

<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>

And now the validation is happening on the entire Phone Number input only. I would like to know how I can make it work on each chip, inside this input setting the min_value to 9 and max_value to 15.

Vuetify - Chips usage: https://vuetifyjs.com/en/components/chips

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

Vee-validate - validation rules: https://baianat.github.io/vee-validate/guide/rules.html

Thank you


回答1:


It seems there isn't build in validation functionality for v-chip. So I am using the default validation(not vee-validate). In that way you can see the results of v-select. You can then loop through the results and validate each value.

        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>



回答2:


For that, we have to

  1. validate the value as array (hence check each item individually)
  2. define vee-validation rule for yourself
// 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

})


来源:https://stackoverflow.com/questions/50519878/vue-vuetify-how-to-make-a-validation-on-each-chip-item-instead-of-the-entire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!