【问题标题】:Veevalidate - Buefy - How can i use the error outside of ValidationProvider?Veevalidate - Buefy - 我如何在 ValidationProvider 之外使用错误?
【发布时间】:2020-04-06 16:17:45
【问题描述】:

我尝试在标签之外使用 ValidationProvider 错误,但没有成功。 这是我的代码:

<ValidationObserver
  ref="obs"
  v-slot="{ errors }"
>
  <b-field
    :type="{ 'is-danger': errors.phone[0] }"
    class="has-margin-bottom-0"
  >
    <div class="control">
      <b-select
        v-model="localPrefix"
        class="no-danger phone-prefix"
      >
        <option
          v-for="phonePrefix in phonePrefixTypes"
          :key="phonePrefix.id"
          :value="phonePrefix.id"
        >
          {{ phonePrefix.prefix }}
        </option>
      </b-select>
    </div>
    <ValidationProvider
      v-slot="{ errors }"
      :vid="name"
      :name="label"
      mode="lazy"
      slim
      :rules="maxLength.validation"
    >
      <b-input
        v-model="localPhone"
        :maxlength="maxLength.length"
        :class="{ 'is-danger': errors.phone[0] }"
        :has-counter="false"
        type="tel"
        expanded
      ></b-input>
    </ValidationProvider>
  </b-field>
  <b-field
    :type="{ 'is-danger': errors.phone[0] }"
    :message="errors.phone"
    class="is-margin-top-0em"
  ></b-field>
</ValidationObserver>

问题是,在这段代码中,errors.phone 此时不存在。 我想我在对提供程序进行任何验证之前尝试使用它,因为如果我使用errors 而不是errors.phone,它可以工作,但它返回对象电话而不是验证错误。

我想我需要初始化errors.phone,但我不知道如何在提供程序之外使用 v-slot 变量。

希望大家能理解我的问题,谢谢大家的帮助!

【问题讨论】:

    标签: forms vue.js vee-validate buefy v-slot


    【解决方案1】:

    我想你已经接近拥有这个权利了。在 ValidationProvider 中,您可以直接使用errors。在 ValidationObserver 内部(但在 VP 外部)您使用errors[&lt;vid&gt;]。在您的示例中,您的 VP 的 vid 实际上是“电话”并不清楚,但假设是,您需要做的就是将您的 VP 代码更改为如下所示:

    <ValidationProvider
      v-slot="{ errors }"
      :vid="name"
      :name="label"
      mode="lazy"
      slim
      :rules="maxLength.validation"
    >
      <b-input
        v-model="localPhone"
        :maxlength="maxLength.length"
        :class="{ 'is-danger': errors }"
        :has-counter="false"
        type="tel"
        expanded
      ></b-input>
    </ValidationProvider>
    

    【讨论】:

    • 嗯,是的,我的副总裁的vid 是“电话”。我在道具中声明了变量,默认情况下是“电话”。我试图在我的 VP 中只使用errors 而不是errors.phone,并继续在外面使用errors.phone。但我很遗憾地告诉你这不起作用......我有同样的错误:TypeError: "errors.phone is undefined"
    • 好的,我明白你现在在说什么了。如果您在 ValidationProvider 之外使用 errors.phone?errors.phone[0]:'' 之类的东西怎么办?
    • 无论哪种方式,我认为这可能是一个错误,所以我为您在库中提交了一个:github.com/logaretm/vee-validate/issues/2546
    • 感谢他的帮助。目前,errors.phone?errors.phone[0]:'' 看起来像是删除了错误的控制台。但我希望 github 请求能给我们一个更好的选择。我不关闭这个问题,等待github。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2019-08-02
    • 2020-08-25
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多