asp.net-mvc - MVC 中的流畅验证 : specify RuleSet for Client-Side validation

标签 asp.net-mvc validation asp.net-mvc-4 unobtrusive-validation fluentvalidation

在我的 ASP.NET MVC 4 项目中,我的 View 模型之一有验证器,其中包含规则集的规则定义。当所有客户端验证通过时,在 Post 操作中使用的 Edit 规则集。 UrlEmail 规则集用于 Edit 规则集(您可以在下面看到)以及仅验证电子邮件和 URL 的特殊 ajax 操作中使用的规则相应地。

我的问题是 View 不知道它应该使用 Edit 规则集来生成客户端 html 属性,并使用 default 规则集,该规则集为空。 如何告诉 View 使用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
        }
    }

View 包含下一个代码:

@{
    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"/> 

最佳答案

在深入研究 FluentValidation 源代码后,我找到了解决方案。要告诉 View 您要使用特定的规则集,请使用 RuleSetForClientSideMessagesAttribute 装饰返回 View 的操作:

[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 重写 Controller 方法:

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
    }
}

关于asp.net-mvc - MVC 中的流畅验证 : specify RuleSet for Client-Side validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908630/

相关文章:

validation - NetSuite/Suitescript - 为什么此验证字段脚本会进入无限循环?

html - 在同一文本字段中验证用户名或电子邮件

asp.net-mvc - MVC 4 : Force ScriptBundle to return non-optimized links of Javascriptfiles

c# - 用于更改可为空和非空数据类型的数据注释是什么?

c# - 将枚举传递给强类型部分 View

javascript - 使用 mvc 模型在 Google Maps API v3 上显示标记

javascript - 使用 window.onload 使用 Javascript 进行表单验证

javascript - 将项目列表从 $.get() 传递到 MVC Controller

c# - MVC4 Controller 将多个 json 对象发布到 Controller ,ajax 发布

c# - ExpertPdf 转换错误 : WebKit Navigation timeout in GetPdfBytesFromUrl