为什么 NotNullValidator 不起作用?

发布于 2024-08-21 18:35:44 字数 705 浏览 10 评论 0原文

我正在使用 Microsoft 企业验证。但在这种情况下,即使我为该属性分配空值,测试也会通过。为什么?

[NotNullValidator(MessageTemplate = "Cannot be null!", Ruleset="validate_x")]
[StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, RangeBoundaryType.Inclusive, Ruleset="validate_x")]
[RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$", MessageTemplate = "Not valid!", Ruleset="validate_x")]
public string x
{
    get;
    set;
}

在测试课中:

[TestMethod()]
public void xTest()
{
    MyBO target = new MyBO() { x = null };
    ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
    Assert.IsTrue(vr.IsValid);
}

所以我知道这是有效的,但它不应该是。 (x 为空!) 有什么想法吗?

谢谢

I'm using Microsoft Enterprise Validation. But in this case the test passes, even if i assign null value to that attribute. Why?

[NotNullValidator(MessageTemplate = "Cannot be null!", Ruleset="validate_x")]
[StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, RangeBoundaryType.Inclusive, Ruleset="validate_x")]
[RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$", MessageTemplate = "Not valid!", Ruleset="validate_x")]
public string x
{
    get;
    set;
}

And in a test class:

[TestMethod()]
public void xTest()
{
    MyBO target = new MyBO() { x = null };
    ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
    Assert.IsTrue(vr.IsValid);
}

So i got that this is valid, but it should not be. (x is null!)
Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-08-28 18:35:44

将您提供的代码复制到简单的控制台应用程序时,我看到 ValidationResults 对象的 IsValid 属性变为 False。我认为你在某个地方做错了什么,但这是不可能的,所以只需查看你的代码即可看到这一点。这是我使用的代码:

public class MyBO
{
    [NotNullValidator(MessageTemplate = "Cannot be null!",
        Ruleset = "validate_x")]
    [StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, 
        RangeBoundaryType.Inclusive, Ruleset = "validate_x")]
    [RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$",
        MessageTemplate = "Not valid!", Ruleset = "validate_x")]
    public string x { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        MyBO target = new MyBO() { x = null };
        ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
        Console.WriteLine(vr.IsValid);
    }
}

When copying the code you supplied to a simple console application I see the IsValid property of the ValidationResults object become False. I think you're doing something wrong somewhere, but it is impossible so see this by just looking at your code. Here is the code I used:

public class MyBO
{
    [NotNullValidator(MessageTemplate = "Cannot be null!",
        Ruleset = "validate_x")]
    [StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, 
        RangeBoundaryType.Inclusive, Ruleset = "validate_x")]
    [RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$",
        MessageTemplate = "Not valid!", Ruleset = "validate_x")]
    public string x { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        MyBO target = new MyBO() { x = null };
        ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
        Console.WriteLine(vr.IsValid);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文