mmcglynn
mmcglynn

Reputation: 7662

Why is ruleSet not recognized in WebForms?

Given this validator:

public ThingValidator()
{
    RuleSet("Subgroup", () =>
    {
        RuleFor(x => x.Apple).NotEmpty();
        RuleFor(x => x.Peach).NotEmpty();
    });
}

According to the documentation, the 'ruleSet' option should use my named ruleset. However, the suleSet symbol cannot be resolved.

var validator = new ThingValidator();
var thing = new Constituent();
var results = validator.Validate(thing, ruleSet: "Subgroup");

What am I missing?

Upvotes: 1

Views: 808

Answers (3)

tiennguyen
tiennguyen

Reputation: 165

This is extension method, declare namespace using FluentValidation and you can use it.

Upvotes: 3

mmcglynn
mmcglynn

Reputation: 7662

I think what you need it:

var results = validator.Validate(constituent, new RulesetValidatorSelector("Subgroup"));

or, closer to the example in the FluentValidation documentation

RulesetValidatorSelector ruleSet = new RulesetValidatorSelector();
var results = validator.Validate(constituent, ruleSet: "Children");

This will work, but ReSharper thinks that the ruleSet local variable is unused.

Upvotes: -1

Dinesh Gurram
Dinesh Gurram

Reputation: 276

I was stuck on this as well, but when I looked into the code, I found that while IValidator<T> has a Validate method, there are also Validate extensions methods in DefaultValidatorExtensions. The call with the ruleSet parameter in @mmcglynn's answer is actually to this extension method from DefaultValidatorExtensions:

public static ValidationResult Validate<T>(
    this IValidator<T> validator, T instance, 
    IValidatorSelector selector = null, 
    string ruleSet = null)

This is why Resharper thinks that the ruleSet variable is unused - because it is not actually passed in. The string "children" passed in is for the 3rd parameter called ruleset, whereas the second parameter (which can take the RulesetValidatorSelector object) defaults to null.

Upvotes: 4

Related Questions