流利的验证.NET可以确定的错误消息序列 [英] Can Fluent Validation .NET determine error messages sequence

查看:191
本文介绍了流利的验证.NET可以确定的错误消息序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用流利的验证.NET用于验证。
是否有可能在验证摘要从RuleFor确定的错误消息序列。

I use Fluent Validation .NET for validating. Is it possible to determine error messages sequence from "RuleFor" in validation summary.

例如:

RuleFor(x=>x.A).NotEmpty().WithMessage("A is required.");
RuleFor(x=>x.B).NotEmpty().WithMessage("B is required.");

例如,我怎么能确定消息序列特异性地显示被要求B。之前需要一个。

For example, How can I determine message sequence to specificly show "B is required." before "A is required".

推荐答案

有规则 FluentValidationModelValidationFactory 验证查询内部没有明确的排序,这意味着错误消息秩序服务器端依赖于规则的申报,如订单如果规则 A 属性规则之前出现了 B ,那么你会在为ValidationResult <见/对 A code>错误消息之前 B 。但它仅适用于手动得到验证结果的(创建验证对象,并调用验证法)。

There is no explicit ordering of rules inside FluentValidationModelValidationFactory validator queries, that means that order of error messages on server-side depends on order of rules declaration, e.g. if rule for A property goes before rule for B, then you will see in ValidationResult error message for A before B. But it works only for manually getting of validation result (create validator object and call Validate method).

在错误进入的ModelState 对象 - 他们也失去了秩序。因为 ModelStateDictionary 类型,它存储的对象作为字典,而不是名单的多数民众赞成。

After errors get into ModelState object - they loss their order. Thats because of ModelStateDictionary type, which stores objects as Dictionary, not as List.

如果我们看看的ValidationSummary 方法NDoc的描述中,我们看到:

And if we look at NDoc description of ValidationSummary method, we see:

返回无序是在ModelStateDictionary对象验证消息的列表(ul元素)。

Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.

但是,如果客户端验证启用 - 然后验证摘要元素出现无服务器调用,这是错误信息订购相同的HTML的输入顺序

But if client-side validation enabled - then validation summary element appears without server call, and it's error messages order the same as order of inputs in html.

结论
只有这样,才能挽救错误信息顺序的ViewResult 是手动的使用验证,打电话验证和手动迭代为ValidationResult 在局部视图或模板来创建你需要的标记。但是,如果你依赖于客户端验证 - 您可以在表单只是reoder输入

Conclusion The only way to save error message order in ViewResult is to 'manually' use validator, call validate and manually iterate ValidationResult in partial view or template to create markup you need. But if you rely on client-side validation — you can just reoder inputs on form.

这篇关于流利的验证.NET可以确定的错误消息序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆