如何在 vee-validate 中显示相关错误消息
如何显示与 vee-validate 中的错误相关的错误消息。我有一个数字输入,我验证它是否为必填和数字,如下所示:
<ValidationObserver :name="'age'">
<div class="flex flex-col items-center">
<h3 class="nunito text-2xl">Wpisz swój rok urodzenia</h3>
<form class="" @submit.prevent="handleSubmit(onSubmit('alko'))">
<label for="age" class="hidden">Wpisz swój rok urodzenia</label>
<ValidationProvider
v-slot="{ errors }"
mode="eager"
rules="numeric|required"
:bails="false"
>
<input
id="age"
type="number"
placeholder="1992"
v-model="ageInput"
name="age"
data-vv-scope="alko"
class="k-border p-4 text-center h-20 w-44 text-xl nunito my-6"
/>
<div class="text-red-500 text-center nunito text-xs">
{{ errors[0] }}
</div>
</ValidationProvider>
</form>
</div>
</ValidationObserver>
<script>
import { ValidationProvider, ValidationObserver, extend } from "vee-validate";
import { numeric, required } from "vee-validate/dist/rules";
export default {
name: "AgeVerificationXs",
components: {
ValidationProvider,
ValidationObserver,
},
data() {
return {
showAlko: true,
ageInput: "",
};
},
methods: {
onSubmit(data) {
console.log(data);
},
},
created() {
extend("required", {
...required,
message: "To pole jest wymagane",
});
extend("numeric", {
...numeric,
message: "To pole może zawierać tylko cyfry",
});
},
};
</script>
问题是,当我在 Firefox 上输入非数字字符时(在 chrome 上,它们在字段类型数字中默认被禁用)时,我收到的消息是字段是必需的,问题是该字段已填充,并且应该应用的规则是“数字”我显示错误[0](根据文档和我找到的每个示例),但我需要显示与错误。我怎样才能做到这一点?感谢任何帮助。 谢谢。
How can I display error message that is relevant to the error in vee-validate. I have a number input and I validate it for required and numeric like so:
<ValidationObserver :name="'age'">
<div class="flex flex-col items-center">
<h3 class="nunito text-2xl">Wpisz swój rok urodzenia</h3>
<form class="" @submit.prevent="handleSubmit(onSubmit('alko'))">
<label for="age" class="hidden">Wpisz swój rok urodzenia</label>
<ValidationProvider
v-slot="{ errors }"
mode="eager"
rules="numeric|required"
:bails="false"
>
<input
id="age"
type="number"
placeholder="1992"
v-model="ageInput"
name="age"
data-vv-scope="alko"
class="k-border p-4 text-center h-20 w-44 text-xl nunito my-6"
/>
<div class="text-red-500 text-center nunito text-xs">
{{ errors[0] }}
</div>
</ValidationProvider>
</form>
</div>
</ValidationObserver>
<script>
import { ValidationProvider, ValidationObserver, extend } from "vee-validate";
import { numeric, required } from "vee-validate/dist/rules";
export default {
name: "AgeVerificationXs",
components: {
ValidationProvider,
ValidationObserver,
},
data() {
return {
showAlko: true,
ageInput: "",
};
},
methods: {
onSubmit(data) {
console.log(data);
},
},
created() {
extend("required", {
...required,
message: "To pole jest wymagane",
});
extend("numeric", {
...numeric,
message: "To pole może zawierać tylko cyfry",
});
},
};
</script>
The thing is that when I enter non-numeric characters on Firefox (on chrome they're disabled by default in fields type number) the message I get is that this field is required, the thing is the field was filled and the rule that should be applied is "numeric" I display errors[0] (accordingly to the docs and every single example I have found) but I need to display error message relevant to the error. How can I do that? Appreciate any help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信问题在于您在created()生命周期挂钩中有“必需”和“数字”规则。您需要将它们移出导出默认 {} 和created() 生命周期挂钩,并移到导入它们的位置下方,如下所示:
I believe the problem is that you have your "required" and "numeric" rules in the created() lifecycle hook. You need to move them up out of export default {} and the created() lifecycle hook and below where you are importing them, like this: