.Net - DataAnnotations - 验证 2 个依赖的日期时间

发布于 2024-08-18 11:35:23 字数 760 浏览 7 评论 0原文

假设我有以下类:

public class Post 
{
    public Date BeginDate { get; set; }

    [Validate2Date(BeginDate, EndDate, ErrorMessage = "End date have to occurs after Begin Date")]
    public Date EndDate { get; set; }
}

public class Validate2Dates : ValidationAttribute
{
    public Validate2Dates(DateTime a, DateTime b)
    { ... }

    public override bool IsValid(object value)
    {
        // Compare date and return false if b < a
    }
}

我的问题是如何使用我的自定义 Validate2Dates 属性,因为我不能这样做:

[Validate2Date(BeginDate, EndDate, ErrorMessage = "End date have to occurs before Begin Date")]

我收到以下错误:

非静态字段、方法或对象需要对象引用 属性 '...Post.BeginDate.get' C:...\Post.cs

Let say I got the following classes:

public class Post 
{
    public Date BeginDate { get; set; }

    [Validate2Date(BeginDate, EndDate, ErrorMessage = "End date have to occurs after Begin Date")]
    public Date EndDate { get; set; }
}

public class Validate2Dates : ValidationAttribute
{
    public Validate2Dates(DateTime a, DateTime b)
    { ... }

    public override bool IsValid(object value)
    {
        // Compare date and return false if b < a
    }
}

My problem is how to use my custom Validate2Dates attribute because I can't do that:

[Validate2Date(BeginDate, EndDate, ErrorMessage = "End date have to occurs before Begin Date")]

I got the following error:

An object reference is required for the non-static field, method, or
property '...Post.BeginDate.get' C:...\Post.cs

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

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

发布评论

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

评论(2

痴骨ら 2024-08-25 11:35:23

您不能使用这样的属性。属性参数仅限于常量值。

我认为更好的解决方案是在您的类上提供一个方法来实现此检查,并且可以通过您喜欢的某些业务逻辑验证接口进行调用。

You can't use an Attribute like that. Attribute parameters are restricted to constant values.

Imo the better solution would be to provide a method on your class that implements this check and can be called through some business logic validation interface of your liking.

流绪微梦 2024-08-25 11:35:23

答案是肯定的,您可以做您想做的事情,但不能按照您当前的方式做。 (顺便说一句,我刚刚注意到这个问题已经得到了很好的回答已经所以我想我至少应该快速引用它。)

基于上面的链接...

  1. 您需要编写一个自定义验证器(您已经完成了)
  2. 您需要装饰您的模型在级别,而不是属性级别
  3. 您不会使用属性本身作为参数 - 相反,您只需将它们引用为要通过反射查找的字符串

[Validate2Date(BeginDate) , EndDate, ...

变为

[Validate2Date(StartDate = "BeginDate", EndDate = "EndDate", ...

然后您将重写 IsValid() 并反映必要的来自链接

.... 
        var properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
        object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
        return Object.Equals(originalValue, confirmValue);
....

The answer is yes, you can do what you're trying to do, just not the way you're currently doing it. (Incidentally, I just noticed that this question has been answered very well already so I thought I'd at least drop a quick reference to it.)

Based on the link above...

  1. You'll need to write a custom validator (which you've already done)
  2. You'll need to decorate your model at the class level, not the property level
  3. You won't be using the properties themselves as parameters - instead you'll simply reference them as strings to be looked up via reflection

[Validate2Date(BeginDate, EndDate, ...

becomes

[Validate2Date(StartDate = "BeginDate", EndDate = "EndDate", ...

You'll then override IsValid() and reflect over the the necessary properties to perform the comparison. From the link

.... 
        var properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
        object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
        return Object.Equals(originalValue, confirmValue);
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文