自定义验证未执行
我在 WPF 应用程序中有一个视图模型类,它具有一些非常复杂的验证。我已经实现了 IValidatableObject 接口来提供自定义验证逻辑。问题是我的 IEnumerable
这是尝试验证的代码: Validator.TryValidateObject(RMA, new ValidationContext(RMA, null, null), result);
知道为什么 Validator
对象没有调用我的自定义验证代码吗?
I have a View Model class in a WPF application that has some very complex validation. I have implemented the IValidatableObject
interface to provide the custom validation logic. The problem is that my IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
is never called!
Here is the code attempting the validation:Validator.TryValidateObject(RMA, new ValidationContext(RMA, null, null), result);
Any ideas why the Validator
object is not calling my custom validation code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我在自定义类中的一个字段上有
[Required]
,并且Validator
在所有数据注释验证完成之前不会执行自定义验证。删除[Required]
允许执行自定义验证。编辑:
验证对象时,在 Validator.ValidateObject 中应用以下过程:
IValidatableObject
,则调用其验证方法并返回任何失败http://jeffhandley.com/archive/2009/10/16 /validator.aspx
验证将在第 2 步中止。
The problem was that I was I had
[Required]
on one of the fields in the custom class and theValidator
will not perform custom validation until all data annotation validation has been completed. Removing the[Required]
allows the custom validation to execute.EDIT:
When validating an object, the following process is applied in Validator.ValidateObject:
IValidatableObject
, then call its Validate method and return any failure(s)http://jeffhandley.com/archive/2009/10/16/validator.aspx
Validation will abort at step #2.