admin管理员组文章数量:1336660
Hey I just starting learning vue 3, vee-validate and yup and I want to display multiple validation errors below input and not just single error message, like this
detail-expected
I tried to use defineField from vee-validate useForm but the docs is not mentioning anything about this usecase
Hey I just starting learning vue 3, vee-validate and yup and I want to display multiple validation errors below input and not just single error message, like this
detail-expected
I tried to use defineField from vee-validate useForm but the docs is not mentioning anything about this usecase
Share Improve this question asked Nov 19, 2024 at 15:35 A DonglaotianA Donglaotian 11 silver badge3 bronze badges 1- Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Nov 20, 2024 at 3:55
1 Answer
Reset to default 0Nevermind I read again vee-validate docs and found out a way to display multiple errors messages, in case you right wondering, here is my code with vue 3 SFC typescript
<template>
<div>
<form @submit.prevent="onSubmit">
<input type="password" v-model="newPassword" v-bind="newPasswordAttrs" placeholder="password" /><br>
<small class="error" v-for="error in newPasswordAttrs.errors">
{{ error }}<br>
</small> <br>
<input type="password" v-model="confirmPassword" v-bind="confirmPasswordAttrs" placeholder="confirm password" /><br>
<small class="error" v-for="error in confirmPasswordAttrs.errors">
{{ error }}<br>
</small> <br>
<button type="submit">Submit</button>
</form>
</div>
</template>
<script setup lang="ts">
import { toTypedSchema } from '@vee-validate/yup';
import { useForm } from 'vee-validate';
import * as yup from 'yup';
const oldPassword = '12345678';
const { handleSubmit, defineField } = useForm({
validationSchema: toTypedSchema(
yup.object({
newPassword: yup
.string()
.required()
.min(8)
.max(16)
.default('')
.test('password', 'Password must be different from the old password', (value) => value !== oldPassword),
confirmPassword: yup
.string()
.required()
.oneOf([yup.ref('newPassword')], 'Passwords must match')
.default('')
})
)
});
const onSubmit = handleSubmit((values) => {
// do something with the values
alert(`values: ${JSON.stringify(values)}`);
});
const [newPassword, newPasswordAttrs] = defineField(
'newPassword',
{
props: (state) => ({
errors: state.errors // here is how I do it
})
}
);
const [confirmPassword, confirmPasswordAttrs] = defineField(
'confirmPassword',
{
props: (state) => ({
errors: state.errors // here is how I do it
})
}
);
</script>
<style scoped>
.error {
color: red;
}
</style>
本文标签:
版权声明:本文标题:typescript - vee-validate & yup: how to display multiple validation errors and not just single error message (fast-exist 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742417857a2471064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论