FluentValidation v3: Other features

This is part 6 in a series of posts about the new features in FluentValidation v3.

In addition to the major new features I’ve already posted about, there are several other smaller features available in FluentValidation v3:

Support for DisplayAttribute

FluentValidation will now automatically infer the display name for properties annotated with the DisplayAttribute/DisplayNameAttribute.

Support for custom arguments in WithLocalizedMessage

Localized error messages now support custom format arguments in the same way as non-localized messages.

Support for validating child properties at the top level

FluentValidation v3 allows rules to be defined for child properties within the top level validator:

RuleFor(x => x.Address.PostalCode).NotNull();

In previous versions of FluentValidation, the property name would always be generated as “PostalCode” instead of “Address.PostalCode”, and you’d have to rely on a child validator which in some situations isn’t desirable.

Additional rules specified on the client

Support for client side error messages has been slightly expanded. The following rules are now supported on the client:

  • NotNull/NotEmpty
  • Matches (regex)
  • InclusiveBetween (range)
  • CreditCard
  • Email
  • EqualTo (cross-property equality comparison)
  • Length

In addition, the client-side error messages for several validators have been improved so they no longer rely on FluentValidation’s named placeholders when running on the client.

Specify a ruleset used to generate client-side rules

If you’re using rulesets alongside ASP.NET MVC, then you’ll notice that by default FluentValidation will only generate client-side error messages for rules not part of any ruleset. You can instead specify that FluentValidation should generate clientside rules from a particular ruleset by attributing your controller action with a RuleSetForClientSideMessagesAttribute:

public ActionResult Index() {
   return View(new PersonViewModel());

…plus several bug fixes.

Written on June 25, 2011