在我的ASP.NET MVC 4项目中,我有一个我的视图模型的验证器,它包含RuleSets的规则定义。传递所有客户端验证时,在Post操作中使用的Edit
规则集。 Url
和Email
规则集使用Edit
规则集中的规则(您可以在下面看到)以及特殊的ajax操作,这些操作仅相应地验证电子邮件和Url。
我的问题是,视图不知道它应该使用Edit
规则集来生成客户端html属性,并使用default
规则集,它是空的。 如何告诉视图使用Edit
规则集生成输入属性?
型号:
public class ShopInfoViewModel
{
public long ShopId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public string Email { get; set; }
}
验证
public class ShopInfoViewModelValidator : AbstractValidator<ShopInfoViewModel>
{
public ShopInfoViewModelValidator()
{
var shopManagementService = ServiceLocator.Instance.GetService<IShopService>();
RuleSet("Edit", () =>
{
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Enter name.")
.Length(0, 255).WithMessage("Name length should not exceed 255 chars.");
RuleFor(x => x.Description)
.NotEmpty().WithMessage("Enter name.")
.Length(0, 10000).WithMessage("Name length should not exceed 10000 chars.");
ApplyUrlRule(shopManagementService);
ApplyEmailRule(shopManagementService);
});
RuleSet("Url", () => ApplyUrlRule(shopManagementService));
RuleSet("Email", () => ApplyEmailRule(shopManagementService));
}
private void ApplyUrlRule(IShopService shopService)
{
RuleFor(x => x.Url)
.NotEmpty().WithMessage("Enter url.")
.Length(4, 30).WithMessage("Length between 4 and 30 chars.")
.Matches(@"[a-z\-\d]").WithMessage("Incorrect format.")
.Must((model, url) => shopService.Available(url, model.ShopId)).WithMessage("Shop with this url already exists.");
}
private void ApplyEmailRule(IShopService shopService)
{
// similar to url rule: not empty, length, regex and must check for unique
}
}
验证操作示例:
public ActionResult ValidateShopInfoUrl([CustomizeValidator(RuleSet = "Url")]
ShopInfoViewModel infoViewModel)
{
return Validation(ModelState);
}
获取并发布ShopInfoViewModel
的操作:
[HttpGet]
public ActionResult ShopInfo()
{
var viewModel = OwnedShop.ToViewModel();
return PartialView("_ShopInfo", viewModel);
}
[HttpPost]
public ActionResult ShopInfo(CustomizeValidator(RuleSet = "Edit")]ShopInfoViewModel infoViewModel)
{
var success = false;
if (ModelState.IsValid)
{
// save logic goes here
}
}
查看包含下一个代码:
@{
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
}
<form class="master-form" action="@Url.RouteUrl(ManagementRoutes.ShopInfo)" method="POST" id="masterforminfo">
@Html.TextBoxFor(x => x.Name)
@Html.TextBoxFor(x => x.Url, new { validationUrl = Url.RouteUrl(ManagementRoutes.ValidateShopInfoUrl) })
@Html.TextAreaFor(x => x.Description)
@Html.TextBoxFor(x => x.Email, new { validationUrl = Url.RouteUrl(ManagementRoutes.ValidateShopInfoEmail) })
<input type="submit" name="asdfasfd" value="Сохранить" style="display: none">
</form>
结果html输入(没有任何客户端验证属性):
<input name="Name" type="text" value="Super Shop"/>
答案 0 :(得分:7)
在挖掘FluentValidation源代码后,我找到了解决方案。要告诉您要使用特定规则集的视图,请使用RuleSetForClientSideMessagesAttribute
修饰返回视图的操作:
[HttpGet]
[RuleSetForClientSideMessages("Edit")]
public ActionResult ShopInfo()
{
var viewModel = OwnedShop.ToViewModel();
return PartialView("_ShopInfo", viewModel);
}
如果需要指定多个规则集 - 使用另一个构造函数重载并使用逗号分隔规则集:
[RuleSetForClientSideMessages("Edit", "Email", "Url")]
public ActionResult ShopInfo()
{
var viewModel = OwnedShop.ToViewModel();
return PartialView("_ShopInfo", viewModel);
}
如果您需要决定将直接使用哪个规则集 - 您可以通过在下一个方向将数组放入HttpContext来破解FluentValidation(RuleSetForClientSideMessagesAttribute
当前不是为了覆盖):
public ActionResult ShopInfo(validateOnlyEmail)
{
var emailRuleSet = new[]{"Email"};
var allRuleSet = new[]{"Edit", "Url", "Email"};
var actualRuleSet = validateOnlyEmail ? emailRuleSet : allRuleSet;
HttpContext.Items["_FV_ClientSideRuleSet"] = actualRuleSet;
return PartialView("_ShopInfo", viewModel);
}
不幸的是,官方文档中没有关于此属性的信息。
<强>更新强>
在最新版本中,我们有动态规则集设置的特殊扩展方法,您应该在操作方法内或OnActionExecuting
/ OnActionExecuted
/ OnResultExecuting
内使用覆盖控制器的方法:
ControllerContext.SetRulesetForClientsideMessages("Edit", "Email");
或在自定义ActionFilter
/ ResultFilter
内:
public class MyFilter: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
((Controller)context.Controller).ControllerContext.SetRulesetForClientsideMessages("Edit", "Email");
//same syntax for OnActionExecuted/OnResultExecuting
}
}
答案 1 :(得分:0)
添加到此,因为库已更新以解决这种情况......
从7.4.0开始,可以根据您的具体情况动态选择一个或多个规则集;
ControllerContext.SetRulesetForClientsideMessages("ruleset1", "ruleset2" /*...etc*);
答案 2 :(得分:0)
有关此文档,可以在最新的FluentValidation网站上找到: https://fluentvalidation.net/aspnet#asp-net-mvc-5
在初始化验证器并自动验证模型时,向操作添加CustomizeValidator属性将在管道内应用规则集。
public ActionResult Save([CustomizeValidator(RuleSet="MyRuleset")] Customer cust) {
// ...
}