FluentValidation 3.3 available

FluentValidation 3.3 is now available to download.

This is a maintenance release contains the following changes:

  • Added additional overload for Must that accepts a PropertyValidatorContext.
  • Allow rules not in a RuleSet to be executed in addition to rules in a ruleset, eg: validator.Validate(instance, ruleSet: "default,MyRuleset");
  • Support AbstractValidator.Custom rules within top level When conditions.
  • Patch that adds error messages localized in Russian
  • Compatibility with ASP.NET MVC 4 Beta (new NuGet package ID is FluentValidation.Mvc4)

FluentValidation 3.2 Available

FluentValidation 3.2 is now available for download.

This is a maintenance release that addresses a couple of bugs from FluentValidation 3.1:

  • The NotEmptyValidator can now be used with collections that don't implement ICollection (such as Entity Framework's EntityCollection<T>)
  • AbstractValidator.Custom can now be used within RuleSets.

FluentValidation 3.1 available

FluentValidation 3.1 is now available for downloaded either from the CodePlex page or through NuGet.

The biggest new feature of 3.1 is being able to share a condition across multiple rules. In previous versions, if you wanted to re-use a condition across multiple rules then you’d have to declare the condition multiple times:

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
        RuleFor(x => x.Surname).NotNull().When(x => x.Id > 0);
        RuleFor(x => x.Forename).NotNull().When(x => x.Id > 0);
    }
}

…but with 3.1 you can declare the condition once using a nested closure:

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
        When(x => x.Id > 0, () => {
           RuleFor(x => x.Surname).NotNull();
           RuleFor(x => x.Forename).NotNull();
        });
    }
}

In addition, FluentValidation’s support for nullable types (which was enhancd with 3.0) has been extended to work with cross-property rules.

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:

[RuleSetForClientSideMessages("MyRuleset")]
public ActionResult Index() {
   return View(new PersonViewModel());
}

…plus several bug fixes.

FluentValidation v3 available

FluentValidation v3 is now available for download. You can grab the binaries from the CodePlex page or by installing it using NuGet:

install-package FluentValidation

If you want integration with ASP.NET MVC 3, you can install the FluentValidation.Mvc3 nuget package:

install-package FluentValidation.Mvc3

For a summary of what’s new in this release, check out the following posts:

…or take a look at the full changelog.

Note that this release takes a dependency on .NET 4, so if you’re still running on .NET 3.5 then you’ll need to stick with FluentValidation 2.0.

View previous posts