首页 > 解决方案 > 将 Vuelidate 与 v-for 和 props 一起使用

问题描述

你们都如何使用 Vuelidate 和v-for. 我似乎无法让它正常工作。它要么验证text-fields我的表单中的每一个,要么引发错误。我只希望它验证正在输入的字段,而不是从v-for.

在下面的模板中,您可以看到credsinamazonCredsArray是从父组件传入的道具。取决于其中的散列amazonCredsArray数是创建的表单数。使用下面的设置,text-field下面会被多次创建,当只输入 1 时,Vuelidate 会验证所有这些字段。我已经:key在现场定义了,但它没有帮助。

模板

<template>
<v-container fluid grid-list-lg class="come_closer">
  <v-layout row wrap>
    <v-flex xs12 v-for="creds in amazonCredsArray" :key="creds.id" class="pb-4">
      <v-card class="lightpurple">
        <v-card-title>
          <v-icon class="my_dark_purple_text">language</v-icon>
          <h1 class="title oswald my_dark_purple_text pl-2 pr-5">ENTER YOUR AMAZON CREDENTIALS BELOW</h1>
        </v-card-title>

     <v-form>
        <v-layout xs12 row wrap class="mx-auto" >
          <v-flex xs12>
            <v-text-field
              :error-messages="sellerIdErrors"
              required
              :key="creds.seller_id"
              color="indigo"
              label="Amazon Seller Id"
              v-model="creds.seller_id"
              prepend-icon="person"
              @input="$v.seller_id.$touch()"
              @blur="$v.seller_id.$touch()"
            ></v-text-field>
          </v-flex>

还有我的违规脚本:

脚本

import { validationMixin } from 'vuelidate';
import { required } from 'vuelidate/lib/validators';

var url = "https://bc-only-rates-trimakas.c9users.io";

export default {
  mixins: [validationMixin],
  validations: {
    seller_id: { required }
  },
  props: ["amazonCredsArray"],
  computed:{
    sellerIdErrors () {
      const errors = []
      if (!this.$v.seller_id.$dirty) return errors
      !this.$v.seller_id.checked && errors.push('Please provide us your seller id')
      return errors
    },
  },

标签: vue.jsvuetify.jsvuelidate

解决方案


推荐阅读