绕过 ASP.NET MVC 2 上的数据注释验证

发布于 2024-09-12 15:38:53 字数 95 浏览 7 评论 0原文

我想知道是否可以绕过使用数据注释的一个属性的验证。由于我在多个页面上使用该模型,因此我需要在某些页面中进行检查,但在其他页面中不需要,因此我希望忽略它。

谢谢!

I would like to know if it's possible to bypass the validation of one property which is using Data Annotations. Since I use the model across multiple pages, there's a check I need in some, but not in others, so I would like it to be ignored.

Thaks!

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

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

发布评论

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

评论(2

┾廆蒐ゝ 2024-09-19 15:38:53

您可以使用 FluentValidation,它用作外部验证器类。在这种情况下,您将为每个场景实现不同的验证器类。

http://fluidvalidation.codeplex.com/

示例:

using FluentValidation;
public class CustomerValidator: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
        RuleFor(customer => customer.Forename).NotEmpty()
            .WithMessage("Please specify a first name");
    }
}

public class CustomerValidator2: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
    }
}

Customer customer = new Customer();

CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

CustomerValidator2 validator2 = new CustomerValidator2();
ValidationResult results2 = validator2.Validate(customer);

results would have 2 validation errors
results2 would have 1 validation error for the same customer

You could use FluentValidation, which uses as external validator class. In this case you would implement a different validator class for each scenario.

http://fluentvalidation.codeplex.com/

Example:

using FluentValidation;
public class CustomerValidator: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
        RuleFor(customer => customer.Forename).NotEmpty()
            .WithMessage("Please specify a first name");
    }
}

public class CustomerValidator2: AbstractValidator<Customer> {
    public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
    }
}

Customer customer = new Customer();

CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

CustomerValidator2 validator2 = new CustomerValidator2();
ValidationResult results2 = validator2.Validate(customer);

results would have 2 validation errors
results2 would have 1 validation error for the same customer
鹤仙姿 2024-09-19 15:38:53

我不相信数据注释可以实现这一点。我知道 Microsoft 企业库验证应用程序块具有用于分组验证的规则集概念。这允许您在多个规则集上验证对象,例如默认规则集和某些页面上的扩展规则集。数据注释没有类似规则集的东西。

这是使用 VAB 的示例:

public class Subscriber
{
    [NotNullValidator]
    [StringLengthValidator(1,200)]
    public string Name { get; set; }

    [NotNullValidator(Ruleset="Persistence")]
    [EmailAddressValidator]
    public string EmailAddress { get; set; }
}

I don't believe this is possible with Data Annotations. I know the Microsoft Enterprise Library Validation Application Block has the notion of rule sets to group validations. This allows you to validate an object on several rule sets, for instance the default ruleset and on some pages the extended rule set. Data Annotations does not have something like rule sets.

Here is an example using VAB:

public class Subscriber
{
    [NotNullValidator]
    [StringLengthValidator(1,200)]
    public string Name { get; set; }

    [NotNullValidator(Ruleset="Persistence")]
    [EmailAddressValidator]
    public string EmailAddress { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文