Vue.js 威利达特酒店;顺风:为什么我总是看到无效的错误?
就我的一生而言,我似乎无法理解为什么我总是看到验证错误。逻辑工作得很好,似乎在我的Vue.js 威利达特酒店;顺风:为什么我总是看到无效的错误?,vue.js,tailwind-css,vuelidate,Vue.js,Tailwind Css,Vuelidate,就我的一生而言,我似乎无法理解为什么我总是看到验证错误。逻辑工作得很好,似乎在我的:类绑定中,我把它都搞错了。我知道当我弄明白这一点的时候,我会觉得自己很傻,但是在无数的教程之后,我觉得我真的是从中复制/粘贴了,我仍然在同一条船上 这是我的电子邮件模板。密码输入是相同的 <div class="relative"> <label for="email" class="sr-only block text-sm font-
:类绑定中,我把它都搞错了。我知道当我弄明白这一点的时候,我会觉得自己很傻,但是在无数的教程之后,我觉得我真的是从中复制/粘贴了,我仍然在同一条船上
这是我的电子邮件模板。密码输入是相同的
<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>
当我输入有效的电子邮件和/或密码时,验证错误就会消失。如果我试图提交无效数据,验证程序工作正常。使用有效的电子邮件/密码,我能够成功提交表单
当页面第一次加载时,我的表单是这样的。如果字段无效,如何正确地呈现表单以仅显示错误
谢谢你的建议
尝试将$v.user.email.$error
条件添加到border类绑定、图标和错误消息中:
<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>
电子邮件地址
电子邮件是必需的
请使用有效的电子邮件地址
提交
这是在输入之前发生的吗?是的,上面的图片是页面加载时发生的情况-没有用户交互。如果我开始输入,验证将按预期进行并清除。
<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>