bootstrap formvalidation.io 带有自定义服务器端的远程验证器返回错误消息
bootstrap formvalidation.io remote validator with custom server side returned error message
我在这里使用 bootstrap formvalidation.io 远程验证器:
http://formvalidation.io/validators/remote/
有什么方法可以在发生验证错误时从服务器端获取错误消息并显示在客户端。
服务器端出现错误后json我return:
{
"valid":false,
"errorMessage":"The format for this postal code has to be in the format of 5 digits"
}
是的,您可以使用远程验证器返回的附加数据,如下所述:http://formvalidation.io/examples/using-data-returned-validator/
当触发 onError
事件时,data.result
包含您的网络服务的 JSON 响应,因此 data.result.errorMessage
是您的消息。
要设置不同的验证器消息,请使用此处描述的 updateMessage()
函数:http://formvalidation.io/api/#update-message
结合起来,您的代码应如下所示:
$("#form")
.formValidation({
// other validator options...
fields: {
field-x: {
validators: {
remote: {
// other remote validator options here
onError: function(e, data) {
$("#form").formValidation("updateMessage", "field-x", "remote", "Error: " + data.result.errorMessage);
}
}
}
}
}
});
我在这里使用 bootstrap formvalidation.io 远程验证器:
http://formvalidation.io/validators/remote/
有什么方法可以在发生验证错误时从服务器端获取错误消息并显示在客户端。
服务器端出现错误后json我return:
{
"valid":false,
"errorMessage":"The format for this postal code has to be in the format of 5 digits"
}
是的,您可以使用远程验证器返回的附加数据,如下所述:http://formvalidation.io/examples/using-data-returned-validator/
当触发 onError
事件时,data.result
包含您的网络服务的 JSON 响应,因此 data.result.errorMessage
是您的消息。
要设置不同的验证器消息,请使用此处描述的 updateMessage()
函数:http://formvalidation.io/api/#update-message
结合起来,您的代码应如下所示:
$("#form")
.formValidation({
// other validator options...
fields: {
field-x: {
validators: {
remote: {
// other remote validator options here
onError: function(e, data) {
$("#form").formValidation("updateMessage", "field-x", "remote", "Error: " + data.result.errorMessage);
}
}
}
}
}
});