类级验证 [英] Class-level validation
问题描述
我在验证类 的DataAnnotations
utils的。
I am validating a class with DataAnnotations
utils.
我有,有一个标题
属性类和一个项目
属性。
我想申请一个<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx"相对=nofollow> RequiredAttribute标签
到标题
属性,但它应该是无效只有当项目
属性为null;如果项目
属性设置一个对象,在标题
不是必需的。
I have a class that has a Title
property and an Item
property.
I want to apply a RequiredAttribute
to the Title
property but it should be invalid only if the Item
property is null; if the Item
property is set with an object, the Title
is not required.
在短的话,我想<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx"相对=nofollow> RequiredAttribute标签
只有当类中的条件满足验证。
In short words, I want the RequiredAttribute
to validate only if a condition in the class is satisfied.
如何才能做到这一点。
由于我没有找到别的办法了,因为我通常不需要此功能,所以通常情况下,我决定把它使用类级别的验证粗糙的方式。 我的问题是那么,有没有办法来手动更新用户界面,以使该标题文本框,红框,即无效呢?
As I didn't find other way, and since I usually don't need this functionality so often, I decided to make it the rough way using a class-level validator. my question is then, is there a way to manually update the UI to make that Title TextBox with a red frame, i.e. to invalidate it?
更新2
我想要的类级验证到现场总结。
例如,我有场成本和SalesPrice,我想确保SalesPrice>成本和SalesPrice否则无效,我不希望在类级别的全球验证错误。
Update 2
I want the class-level validator to summarize on a field.
For example, I have to fields Cost and SalesPrice, I wanna make sure that SalesPrice > Cost and invalidate the SalesPrice otherwise, I don't want a global validation error on the class level.
我preFER做了xamly方式。
I prefer to do it the xamly way.
推荐答案
您可以通过创建该类的自定义验证属性来做到这一点。分配给特性不幸的是DataAnnotation属性不能访问父类的其他属性,据我所知,因此需要创建一个类的验证。
使用System.ComponentModel.DataAnnotations命名空间,你需要创建你的自定义属性类从ValidationAttribute继承并重写的IsValid方法(我没有测试过下面的code,但它应该让你去):
You may be able to do this by creating a custom validation attribute for the class. Unfortunately DataAnnotation attributes assigned to properties cannot access other properties of the parent class as far as I am aware hence the need to create a class validator.
Using the System.ComponentModel.DataAnnotations namespace you will need to create you custom attribute class inheriting from ValidationAttribute and override the IsValid method (I have not tested the code below but it should get you going):
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
sealed public class CustomAttribute: ValidationAttribute
{
public CustomAttribute()
{
}
public override bool IsValid(object value)
{
if(value is myClass)
{
return ((myClass)value).Item != null &&
string.IsNullOrEmpty(((myClass)value).Title) ? false : true;
}
else return true;
}
}
挖掘远一点看来,虽然交叉领域验证它不可能开箱就可以通过扩展来支持它的框架来实现的。请参见这篇文章为详细信息,希望这将被添加到MVC的未来版本。
Digging a little further it appears that whilst cross field validation it not possible out of the box it can be achieved by extending the framework to support it. See this article for details, hopefully this will be added to future versions of MVC.
这篇关于类级验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!