Can Fluent Validation.NET determine the sequence of error messages
I am using Fluent Validation.NET for validation. Is it possible to determine the sequence of error messages from "RuleFor" in the check summary.
Example:
RuleFor(x=>x.A).NotEmpty().WithMessage("A is required.");
RuleFor(x=>x.B).NotEmpty().WithMessage("B is required.");
For example, How to define a sequence of messages to indicate that "B is required". before "A" is required.
source to share
There is no explicit ordering of rules within FluentValidationModelValidationFactory
validator requests , which means that the order of error messages on the server side depends on the order in which the rules are declared, for example. if a rule for a property A
goes before a rule for B
, you will see an error message ValidationResult
for A
before B
. But it only works to get the validation result manually (create a validator object and call the method Validate
).
After the errors hit the object ModelState
, they lose their order. This is because of the type ModelStateDictionary
that stores objects as a Dictionary and not as a List.
And if we look at the description of the NDoc method ValidationSummary
, we see:
Returns an unordered list (ul element) of validation messages that are in a ModelStateDictionary object.
But if client side validation is enabled - then the validation summary item appears without calling the server and the error messages will be the same as the order of the html entries.
Conclusion
The only way to keep the error messages in order ViewResult
is to "manually" use a validator, validate and manually iterate through ValidationResult
the partial view or template to create the markup you need. But if you rely on client side validation - you can simply enter data into the recorder over the form.
source to share