使用 FluentValidation 时向类添加 DataAnnotation
我使用 FluentValidation 框架向 MVC 项目中的模型添加验证和注释。
我需要将数据注释添加到模型的类级别。即模型需要添加 DisplayColumn 属性。但是,由于我使用 FluentValidation(并且将应用程序的 ModelMetadataProvider 设置为使用 FluentValidation),即使我将 DisplayColumn 属性放在模型类上,也不会使用它。但是,我找不到使用 FluentValidation 添加该注释的方法。
有谁知道我怎样才能让它发挥作用?
谢谢
I use the FluentValidation framework to add validation and annotations to my models in an MVC project.
I need to add data annotations to the class level of a model. Namely, the model needs to have the DisplayColumn attribute added. But, since I use FluentValidation (and have the application's ModelMetadataProvider set to use FluentValidation), even if I put the DisplayColumn attribute on the model class, it isn't used. However, I can't find a way to add that annotation by using FluentValidation.
Does anyone have any idea how I can get that to work?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上并不建议使用 FluentValidationModelMetadataProvider - 这实际上只是一个实验性添加(很可能会从下一个版本中删除),并且它不支持任何类级别的 DataAnnotations(例如 DisplayColumn)。我建议您仅使用 FluentValidation 进行验证,但坚持使用元数据的属性。
话虽这么说,如果您真的想让这个工作正常,那么您可以通过使用仅用于元数据的自定义无操作验证器来实现:
...然后您可以像这样使用它:
然后您需要创建一个自定义ModelMetadataProvider 知道如何处理此问题:
提供程序重写 GetMetadataForModel 方法并查找使用 DisplayColumn 的任何属性。如果您也想支持任何其他自定义元数据扩展,则可以扩展此功能。然后,您可以使用此提供程序来代替 FluentValidation 附带的元数据提供程序。
但是,我仍然不推荐这种方法...该库是为执行验证而设计的,而不是为了生成 UI 元数据。
I wouldn't actually recommend using the FluentValidationModelMetadataProvider - this was only really ever an experimental addition (which very well may be removed from the next release), and it doesn't support any of the class-level DataAnnotations (such as DisplayColumn). I would suggest that you use FluentValidation only for validation, but stick with attributes for metadata.
That being said, if you really want to get this working then you could implement by using a custom no-op validator that's used only for metadata:
... Which you could then use like this:
You'd then need to create a custom ModelMetadataProvider that knows how to process this:
The provider overrides the GetMetadataForModel method and looks for any properties that use DisplayColumn. This could probably be extended if you want to support any other custom metadata extensions too. You could then use this provider in place of the metadata provider that comes with FluentValidation.
However, I still wouldn't recommend this approach...the library is designed for performing validation, not for generating UI metadata.