ASP.NET MVC 2 中的验证

发布于 2024-09-25 05:32:48 字数 707 浏览 0 评论 0原文

我在 ASP.NET MVC 2 中使用数据注释进行验证时遇到一些问题。例如,我有 Address 类:

public class Address
{
    public long Id { get; set; }

    [Required]
    public string City { get; set; }

    [Required]
    public string PostalCode { get; set; }

    [Required]
    public string Street { get; set; }
}

和 Order 类:

public class Order
{
    public long Id { get; set; }

    public Address FirstAddress { get; set; }

    public Address SecondAddress { get; set; }

    public bool RequireSecondAddress { get; set; }
}

我想始终验证 Order.FirstAddress,但仅​​当 Order.SecondAddress 时才应验证 Order.SecondAddress。 RequireSecondAddress 设置为 true。

有什么想法吗? :)

克里斯

I have some problems with validation using Data Annotations in ASP.NET MVC 2. For example, I have Address class:

public class Address
{
    public long Id { get; set; }

    [Required]
    public string City { get; set; }

    [Required]
    public string PostalCode { get; set; }

    [Required]
    public string Street { get; set; }
}

And Order class:

public class Order
{
    public long Id { get; set; }

    public Address FirstAddress { get; set; }

    public Address SecondAddress { get; set; }

    public bool RequireSecondAddress { get; set; }
}

I want to validate Order.FirstAddress all the time, but Order.SecondAddress should be validated only if Order.RequireSecondAddress is set to true.

Any ideas? :)

Chris

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

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

发布评论

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

评论(2

万水千山粽是情ミ 2024-10-02 05:32:48

使用数据注释几乎是不可能的,否则需要编写依赖反射的丑陋代码等......(我想你明白了)。

我建议您查看 FluentValidation。它与集成良好ASP.NET MVC。您的验证逻辑可能如下所示:

public class AddressValidator : AbstractValidator<Address>
{
    public AddressValidator()
    {
        RuleFor(x => x.City)
            .NotEmpty();
        RuleFor(x => x.PostalCode)
            .NotEmpty();
        RuleFor(x => x.Street)
            .NotEmpty();
    }
}

public class OrderValidator : AbstractValidator<Order>
{
    public OrderValidator()
    {
        RuleFor(x => x.FirstAddress)
            .SetValidator(new AddressValidator());
        RuleFor(x => x.SecondAddress)
            .SetValidator(new AddressValidator())
            .When(x => x.RequireSecondAddress);
    }
}

您还将受益于拥有一个单独的验证层,该层也可以是 以非常优雅的方式进行单元测试

That's close to impossible using data annotations or it will require writing ugly code that relies on reflection, etc... (I think you get the point).

I would recommend you looking at the FluentValidation. It has a good integration with ASP.NET MVC. Here's how your validation logic might look like:

public class AddressValidator : AbstractValidator<Address>
{
    public AddressValidator()
    {
        RuleFor(x => x.City)
            .NotEmpty();
        RuleFor(x => x.PostalCode)
            .NotEmpty();
        RuleFor(x => x.Street)
            .NotEmpty();
    }
}

public class OrderValidator : AbstractValidator<Order>
{
    public OrderValidator()
    {
        RuleFor(x => x.FirstAddress)
            .SetValidator(new AddressValidator());
        RuleFor(x => x.SecondAddress)
            .SetValidator(new AddressValidator())
            .When(x => x.RequireSecondAddress);
    }
}

You will also benefit from having a separate validation layer which also could be unit tested in a very elegant way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文