How to write global custom validation in vue3? #4098
Purnangshu261978
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
0
Presently working in vue3 and using the validation as below -
const validationRule = object({
name: string()
.required("This field is required")
.max(128, maxMessage("Student Name", 128))
.test("Check-unique-field", function (Fieldvalue) {
const checkValidation = uniquenessValidate(Fieldvalue, targetUniqueNames);
if (!checkValidation ) {
return this.createError({
path: this.path,
message: uniqueMessage("Student Name")
});
} else {
return true;
}
})
In this scenario, I write a custom validation that is uniquenessValidate using the function in form. This is working absolutely fine but the problem this code need to repeatedly using for each and every form to check the uniqueness which I not want to do. I want to write this function in a global places and uses it whenever I need to use the function to call by. Now the problem is when I written the code globally it create to issues to pass the this.createError and this.path . Can we get an idea to write this function globally?
Writing the global function I tried out as below -
import {
uniqueMessage
} from "@/core/libs/test-veevalidate-validations";
import { uniquenessValidate} from "@/core/libs/ test-veevalidate-validator";
import { computed, ComputedRef, defineComponent, ref, toRef } from "vue";
export function fn_uniqueValidator( fieldValue:string, targetValue: ComputedRef<(string | null | undefined)[]>){
const checkValidation = uniquenessValidate(fieldValue, targetValue );
if (!checkValidation ) {
return this.createError({
path:this.path,
message: uniqueMessage("Student Name")
});
} else {
return true;
}
}
Beta Was this translation helpful? Give feedback.
All reactions