Hi, I'm implementing a wizard where I need to validate if exist an object in my database before go to the next step, but it doesn't work properly
This is my code:
const existClient = function () {
return {
validate: function (input) {
const value = input.value;
if (value === "") {
return {
valid: true,
};
}
if (value.length > 0) {
$.ajax({
url: "/clientes/findClient",
type: "GET",
data: {
"id": value
},
success: function (data) {
if(data.length >0){
return {
valid: false,
};
}
});
}
return {
valid: true,
};
},
};
validator = FormValidation.formValidation(
form,
{
fields: {
//some fields
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap: new FormValidation.plugins.Bootstrap5({
rowSelector: ".fv-row",
eleInvalidClass: "",
eleValidClass: ""
}),
excluded: new FormValidation.plugins.Excluded({
excluded: function (field, ele, eles) {
if (form.querySelector("[name="" + field + ""]") === null) {
return true;
}
},
}),
}
}
).registerValidator("existClient", existClient);
I'm glad to hear that it worked perfectly for you! If you have any more questions or need further assistance, feel free to reach out.
HI Yasniel Alejandro
The validate function in your existClient validator is expected to return an object synchronously, but AJAX requests are asynchronous.
To handle this, you should use a promise within your validate function. Here’s how you can modify your code:
https://gist.github.com/KeenthemesHub/6c71fa8566bc0dcd1abaa8d44d522c0e
Hi, Faizal
Worked perfectly, thank's a lot