xVal,整个类的 DataAnnotations

发布于 2024-08-03 19:53:58 字数 1563 浏览 2 评论 0原文

我对一个对象进行了完整的验证,并试图找出处理它的最佳方法。

给定以下类:

public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

我想使用 dataannotation 和 xval 以便在保留该类之前对其进行验证,但我有复杂的验证,需要多个属性。

伪:

if SendBlankCart {
 - no validation on ToName, ToAddress 
} else {
 ToName - required.
 ToAddress - required. 
}

我想这样注释:

[LetterResponseValidator]
public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

并有一个这样的验证规则:

public class LetterResponseValidator : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value.GetType() == typeof(LetterResponse))
            {
                var letterResponse = (letterResponse) value;
                if (letterResponse.SendBlankCard)
                {
                    return true;
                } else
                {
                    if (string.IsNullOrEmpty(letterResponse.FromDisplayName) || string.IsNullOrEmpty(letterResponse.ToAddress1))
                    {
                        return false;
                    }
                    return true;
                }

            }
            return false;
        }
    }

我期望参数是 LetterResponse 类的实例,但它永远不会在我的验证运行器上被调用?

有谁知道如何处理这个问题?

谢谢,

哈尔

I have a complete validation on an obeject and am trying to figure out the best way to handle it.

Given the following class:

public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

I want to use a dataannotation and xval in order to validate the class before I persist it, but I have complex validation that requires more than one property.

Pseudo:

if SendBlankCart {
 - no validation on ToName, ToAddress 
} else {
 ToName - required.
 ToAddress - required. 
}

I would like to annotate like this:

[LetterResponseValidator]
public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

and have a validation rule like this:

public class LetterResponseValidator : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value.GetType() == typeof(LetterResponse))
            {
                var letterResponse = (letterResponse) value;
                if (letterResponse.SendBlankCard)
                {
                    return true;
                } else
                {
                    if (string.IsNullOrEmpty(letterResponse.FromDisplayName) || string.IsNullOrEmpty(letterResponse.ToAddress1))
                    {
                        return false;
                    }
                    return true;
                }

            }
            return false;
        }
    }

I am expecting the parameter to be my instance of the LetterResponse class, but it never gets called on my validation runner?

Does anyone know a way to handle this?

Thanks,

Hal

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

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

发布评论

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

评论(1

兔姬 2024-08-10 19:53:58

我认为这与您拥有类级别验证器这一事实无关。要排除任何连接,请尝试将所需的虚拟验证器应用于“ToName”,并查看验证器是否被调用。

如果它被调用,那么请告诉我,如果没有,那么您应该检查您是否已使用 Global.asax.cs 文件中的 DataAnnotationsModelBinder 覆盖您的标准模型绑定器:

ModelBinders.Binders.DefaultBinder = new DataAnnotationsModelBinder();

有关此完整工作演示项目的更多详细信息,请阅读这有关客户端验证的博客文章

I don't think this has to do with the fact that you have a class-level validator. To exclude any connection, try applying a dummy required validator to "ToName" and see if the validator is called or not.

If it is being called, then let me know, if it's not, then you should check if you have overriden your standard modelbinder with the DataAnnotationsModelBinder in your Global.asax.cs file:

ModelBinders.Binders.DefaultBinder = new DataAnnotationsModelBinder();

For more details on this and fully working demo project, read this blog article about client-side validation.

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