自定义验证未执行

发布于 2024-12-01 05:13:08 字数 353 浏览 7 评论 0原文

我在 WPF 应用程序中有一个视图模型类,它具有一些非常复杂的验证。我已经实现了 IValidatableObject 接口来提供自定义验证逻辑。问题是我的 IEnumerableValidate(ValidationContext validContext) 永远不会被调用!

这是尝试验证的代码: 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 技术交流群。

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

发布评论

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

评论(1

冰雪梦之恋 2024-12-08 05:13:08

问题是我在自定义类中的一个字段上有 [Required] ,并且 Validator 在所有数据注释验证完成之前不会执行自定义验证。删除[Required] 允许执行自定义验证。

编辑:

验证对象时,在 Validator.ValidateObject 中应用以下过程:

  1. 验证属性级属性
  2. 如果任何验证器无效,则中止验证并返回失败
  3. 验证对象级属性
  4. 如果任何验证器无效,则中止返回失败的验证
  5. 如果在桌面框架上并且对象实现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 the Validator 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:

  1. Validate property-level attributes
  2. If any validators are invalid, abort validation returning the failure(s)
  3. Validate the object-level attributes
  4. If any validators are invalid, abort validation returning the failure(s)
  5. If on the desktop framework and the object implements 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.

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