So I am looking to create custom validation of some form fields and need an idea how to approach this. Validation will connect to external api providers and check if data is unique.
因此,我希望创建一些表单字段的自定义验证,并需要了解如何实现这一点。验证将连接到外部api提供程序并检查数据是否惟一。
This takes some time, so I was thinking to create validation in multiple threads with help of async controller(async, await).
这需要一些时间,所以我想在异步控制器(async controller, async wait)的帮助下在多个线程中创建验证。
So when I click submit button it connects to 3 different external apis and checks the data asynchronously.
因此,当我单击submit按钮时,它连接到3个不同的外部api,并异步地检查数据。
What would be the best practice here? Or should I just create customValidation with inheriting ValidationAttribute ?
这里最好的做法是什么?或者我应该只创建带有继承ValidationAttribute的customValidation ?
Thanks.
谢谢。
2 个解决方案
#1
1
You can use RemoteAttribute
. This attribute validates the input by calling an action in your MVC controller.
您可以使用RemoteAttribute。此属性通过调用MVC控制器中的操作来验证输入。
For example, we validate that name is unique
例如,我们验证这个名称是唯一的。
public class SampleModel
{
[Remote("ValidateName" /*action*/, "Home" /*controller*/)]
public string Name { get; set; }
}
The ValidateName action
ValidateName行动
public class HomeController : Controller
{
public async Task<JsonResult> ValidateName(string name)
{
//the external validator
var externalValidators = new Func<string, Task<bool>>[]
{
ExternalValidator1,
ExternalValidator2,
ExternalValidator3
};
//execute each asynchronously and wait for all to finish
var externalValidatorTasks = externalValidators.Select(i => i(name)).ToArray();
await Task.WhenAll(externalValidatorTasks);
//return "invalid!" if any validation failed
if (externalValidatorTasks.Any(i => !i.Result))
{
return Json("invalid!", JsonRequestBehavior.AllowGet);
}
//name is valid
return Json(true, JsonRequestBehavior.AllowGet);
}
//mock external validation
public Task<bool> ExternalValidator1(string name)
{
return Task.FromResult(true);
}
}
#2
0
You can create custom data validation DataAnnotation for this. In server side part of the validation attribute you can invoke the external APIs and in the client side part (validation adapter written in js/jQuery) make asynchornous calls to the external APIs.
您可以为此创建自定义数据验证DataAnnotation。在验证属性的服务器端部分,您可以调用外部api,在客户端部分(用js/jQuery编写的验证适配器)对外部api进行异步调用。
You can check this for your reference: http://programersnotebook.blogspot.in/2013/03/customizing-validation-attributes-in-mvc.html
您可以查看此信息,以供您参考:http://proamersnotebook.blogspot.in/2013/03/customizing-validationing-attributes-in -mvc.html。
#1
1
You can use RemoteAttribute
. This attribute validates the input by calling an action in your MVC controller.
您可以使用RemoteAttribute。此属性通过调用MVC控制器中的操作来验证输入。
For example, we validate that name is unique
例如,我们验证这个名称是唯一的。
public class SampleModel
{
[Remote("ValidateName" /*action*/, "Home" /*controller*/)]
public string Name { get; set; }
}
The ValidateName action
ValidateName行动
public class HomeController : Controller
{
public async Task<JsonResult> ValidateName(string name)
{
//the external validator
var externalValidators = new Func<string, Task<bool>>[]
{
ExternalValidator1,
ExternalValidator2,
ExternalValidator3
};
//execute each asynchronously and wait for all to finish
var externalValidatorTasks = externalValidators.Select(i => i(name)).ToArray();
await Task.WhenAll(externalValidatorTasks);
//return "invalid!" if any validation failed
if (externalValidatorTasks.Any(i => !i.Result))
{
return Json("invalid!", JsonRequestBehavior.AllowGet);
}
//name is valid
return Json(true, JsonRequestBehavior.AllowGet);
}
//mock external validation
public Task<bool> ExternalValidator1(string name)
{
return Task.FromResult(true);
}
}
#2
0
You can create custom data validation DataAnnotation for this. In server side part of the validation attribute you can invoke the external APIs and in the client side part (validation adapter written in js/jQuery) make asynchornous calls to the external APIs.
您可以为此创建自定义数据验证DataAnnotation。在验证属性的服务器端部分,您可以调用外部api,在客户端部分(用js/jQuery编写的验证适配器)对外部api进行异步调用。
You can check this for your reference: http://programersnotebook.blogspot.in/2013/03/customizing-validation-attributes-in-mvc.html
您可以查看此信息,以供您参考:http://proamersnotebook.blogspot.in/2013/03/customizing-validationing-attributes-in -mvc.html。