Vuelidate & Tailwind:为什么我总是看到无效错误?

Vuelidate & Tailwind: Why am I always seeing invalid errors?

我似乎无法理解为什么我总是看到验证错误。逻辑运行良好,似乎都在我的 :class 绑定中,我完全错了。我知道当我弄清楚这个问题时我会觉得自己很傻,但是经过无数教程后,我觉得我已经 copy/paste 了,我仍然在同一条船上。

这是我的电子邮件模板。密码输入相同...

<div class="relative">
    <label for="email" class="sr-only block text-sm font-medium leading-5 text-gray-700">
        Email address
    </label>
    <input type="email"
        id="email"
        v-model.trim="$v.user.email.$model"
        :class="{ 'border-red-500': !$v.user.email.required}"
        placeholder="Email"
        class="mt-1 placeholder-gray-400 shadow appearance-none border rounded w-full py-3 px-3 text-gray-700 leading-tight transition duration-150 ease-in-out sm:text-sm sm:leading-5 focus:outline-none focus:border-gray-400 focus:shadow-outline-gray"
    />
    <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none" 
        v-if="!$v.user.email.required">
              <svg
                  class="h-5 w-5 text-red-500"
                  fill="currentColor"
                  viewBox="0 0 20 20"
              >
                <path
                    fill-rule="evenodd"
                    d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
                    clip-rule="evenodd"
                />
              </svg>
            </div>
        </div>
    <div>
    <p class="mt-2 text-sm text-red-600" v-if="!$v.user.email.required">
        Email is required
    </p>
    <p class="mt-2 text-sm text-red-600" v-if="!$v.user.email.email">
        Please use a valid email address
    </p>
</div>

这是我的脚本块的样子:

data: () => ({
    errors: false,
    user: {
      email: "",
      password: "",
    },
  }),


validations: {
    user: {
      email: { required, email },
      password: { required },
    },
  },

methods: {
    async handleSignIn() {
      this.$v.user.$touch();
      this.empty = !this.$v.user.$anyDirty;
      this.errors = this.$v.user.$anyError;

      if (this.$v.$invalid) {
        this.errors = true;
      } else {
        try {
            ...
        }
      }
   }

当我输入有效的电子邮件 and/or 密码时,验证错误消失了。如果我尝试提交无效数据,则验证器工作正常。使用有效 email/password,我能够成功提交表单。

首次加载页面时,我的表单如下所示。如果字段无效,我如何才能正确呈现表单以仅显示错误?

感谢您的任何建议!

尝试在边框class绑定中添加$v.user.email.$error条件,图标和错误信息:

<div id="app">
  <div class="relative">
    <label for="email" class="sr-only block text-sm font-medium leading-5 text-gray-700">
      Email address
    </label>
    <input type="email" id="email" v-model.trim="$v.user.email.$model" :class="{ 'border-red-500': $v.user.email.$error}" placeholder="Email" class="mt-1 placeholder-gray-400 shadow appearance-none border rounded w-full py-3 px-3 text-gray-700 leading-tight transition duration-150 ease-in-out sm:text-sm sm:leading-5 focus:outline-none focus:border-gray-400 focus:shadow-outline-gray" />
    <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none" v-if="!$v.user.email.required">
      <svg v-if="$v.user.email.$error" class="h-5 w-5 text-red-500" fill="currentColor" viewBox="0 0 20 20">
        <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
      </svg>
    </div>
  </div>
  <div>
    <p class="mt-2 text-sm text-red-600" v-if="$v.user.email.$error">
      Email is required
    </p>
    <p class="mt-2 text-sm text-red-600" v-if="!$v.user.email.email">
      Please use a valid email address
    </p>
    <button class="rounded w-full flex justify-center text-gray-100 bg-purple-700 hover:bg-purple-800 px-6 py-2 flex items-center">
      Submit
    </button>
  </div>
</div>

LIVE DEMO