开发者

validation application block object validator not working with rulesets

Ive been battling with this issue which I originally thought m开发者_如何转开发ay have been to do with polymorphism/inheritance affecting my validation but ive narrowed it to this...

Here is the class structure..

public class Employee {

    [ObjectValidator(Ruleset = "A")]
    public EmployeeName Name { get; set; }

    public Employee()
    {
        Name = new EmployeeName();
    }
}

public class EmployeeName
{
    [StringLengthValidator(1,20,Ruleset = "A")]
    public string First { get; set; }

    public string Last { get; set; }

    public EmployeeName()
    {
        First = string.Empty;
        Last = string.Empty;
    }
}

The test:

[TestFixture]
public class ObjectValidationWithRulesets
{
    [Test]
    public void wont_validate_with_a_ruleset()
    {
        var person = new Employee()
        {
            Name = new EmployeeName()
            {
                First = string.Empty, 
                Last = string.Empty
            }
        };

        var ruleSetValidator =
            ValidationFactory.CreateValidator<Employee>("A");

        var validationResults = ruleSetValidator.Validate(person);

        Assert.That(!validationResults.IsValid,
            "Validation with rulsets failed");
    }
}

This test passes if I remove remove the Ruleset stuff. But once the ruleset is applied, I cant get the object to validate correctly.

Can anyone shed some light on this?

Cheers,


I too had this problem, but I did not define targetRuleSet in the configuration file. I fixed the issue by correcting the way way I was declaring the ObjectValidator attribute . The correct syntax that worked for me is the following

// Correct
[ObjectValidator("RuleSetA", Ruleset = "RuleSetA")]

In my code I had wrongly declared it as follows

// Wrong syntax
[ObjectValidator(Ruleset = "RuleSetA")]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜