.Net MVC2 如何在使用自定义 ValidationAttribute 时向 ModelState 添加错误
我有以下 ValidationAttribute 类,
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class DateValidationAttribute : ValidationAttribute
{
public DateValidationAttribute(string leftDateProperty, CompareOperator compareOperator, string rightDateProperty, string errorMessage)
: base(errorMessage)
{
LeftDateProperty = leftDateProperty;
Operator = compareOperator;
RightDateProperty = rightDateProperty;
}
...
...
}
它在构造函数中需要两个日期属性名称和一个运算符。
在验证方法中,返回语句 LeftDate Operator RightDate 的结果。
public override bool IsValid(object value)
{
DateTime leftDate;
DateTime rightDate;
// Get all properties on the view model
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
DateTime rightDate = (DateTime)properties.Find(RightDateProperty, true).GetValue(value);
DateTime leftDate = (DateTime)properties.Find(LeftDateProperty, true).GetValue(value);
// Perform rule check
switch (Operator)
{
case CompareOperator.Equal:
return leftDate.Equals(rightDate);
case CompareOperator.Greater:
return leftDate > rightDate;
case CompareOperator.Lesser:
return leftDate < rightDate;
case CompareOperator.GreaterOrEqual:
return leftDate >= rightDate;
case CompareOperator.LesserOrEqual:
return leftDate <= rightDate;
default:
return false;
}
}
因为这是一个 AttriuteTargets.Class 属性,我知道框架不可能知道哪个属性导致验证失败。但我知道是 Left Date 属性失败了,因此我想将模型状态中的错误 ID 设置为此属性。这样做的原因是我希望在表单中标记失败的字段。
问题:如何修改添加到ModelState错误集合中的错误项,使其id对应表单中的特定字段?
I have the following ValidationAttribute class
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class DateValidationAttribute : ValidationAttribute
{
public DateValidationAttribute(string leftDateProperty, CompareOperator compareOperator, string rightDateProperty, string errorMessage)
: base(errorMessage)
{
LeftDateProperty = leftDateProperty;
Operator = compareOperator;
RightDateProperty = rightDateProperty;
}
...
...
}
It takes two date property names and an operator in the constructor.
In the validation method the result of the statement LeftDate Operator RightDate is returned.
public override bool IsValid(object value)
{
DateTime leftDate;
DateTime rightDate;
// Get all properties on the view model
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
DateTime rightDate = (DateTime)properties.Find(RightDateProperty, true).GetValue(value);
DateTime leftDate = (DateTime)properties.Find(LeftDateProperty, true).GetValue(value);
// Perform rule check
switch (Operator)
{
case CompareOperator.Equal:
return leftDate.Equals(rightDate);
case CompareOperator.Greater:
return leftDate > rightDate;
case CompareOperator.Lesser:
return leftDate < rightDate;
case CompareOperator.GreaterOrEqual:
return leftDate >= rightDate;
case CompareOperator.LesserOrEqual:
return leftDate <= rightDate;
default:
return false;
}
}
Because this is an AttriuteTargets.Class attribute I know it is impossible for the framework to know which property that is causing the validation to fail. But I know that it is Left Date Property that is failing and therefore I want to set the Id of the error in the modelstate to this property. The reason for this is that I want the failing field to be marked in the form.
Question: How can I modify the error item added to the error collection in ModelState so that its id corresponds to a specific field in the form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种使用 IDataErrorInfo 执行此操作的更好方法,
这就是我的做法。它不像问题中的例子那么通用。使用此解决方案,您必须对每项检查进行编码。但现在验证将一直很好地工作到 javascript 并突出显示失败的输入元素。
I found a better way of doing this using IDataErrorInfo
Here is how I'm doing it. It's not that generic as the example in the question. With this solution you have to code each check. But now the validation will work nice all the way up to the javascript and highlighting of the failing input element.