无法从 MVC2 中的自定义验证属性设置成员名称
我通过子类化 ValidationAttribute 创建了一个自定义验证属性。该属性在类级别应用于我的视图模型,因为它需要验证多个属性。
我正在覆盖
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
并返回:
new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
在 DateOfBirth 是我的视图模型上的属性之一的所有情况下。
当我运行我的应用程序时,我可以看到它受到了影响。 ModelState.IsValid 正确设置为 false,但是当我检查 ModelState 内容时,我发现属性 DateOfBirth 不包含任何错误。相反,我有一个值为 null 的空字符串 Key 和一个包含我在验证属性中指定的字符串的异常。
这会导致使用 ValidationMessageFor 时我的 UI 中不会显示错误消息。如果我使用 ValidationSummary,那么我可以看到错误。这是因为它与属性无关。
看起来好像它忽略了我在验证结果中指定了成员名称的事实。
这是为什么?我该如何解决?
根据要求提供示例代码:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ExampleValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// note that I will be doing complex validation of multiple properties when complete so this is why it is a class level attribute
return new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
}
}
[ExampleValidation]
public class ExampleViewModel
{
public string DateOfBirth { get; set; }
}
I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied to my viewmodel at the class level as it needs to validate more than one property.
I am overriding
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
and returning:
new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
in all cases where DateOfBirth is one of the properties on my view model.
When I run my application, I can see this getting hit. ModelState.IsValid is set to false correctly but when I inspect the ModelState contents, I see that the Property DateOfBirth does NOT contain any errors. Instead I have an empty string Key with a value of null and an exception containing the string I specified in my validation attribute.
This results in no error message being displayed in my UI when using ValidationMessageFor. If I use ValidationSummary, then I can see the error. This is because it is not associated with a property.
It looks as though it is ignoring the fact that I have specified the membername in the validation result.
Why is this and how do I fix it?
EXAMPLE CODE AS REQUESTED:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ExampleValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// note that I will be doing complex validation of multiple properties when complete so this is why it is a class level attribute
return new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
}
}
[ExampleValidation]
public class ExampleViewModel
{
public string DateOfBirth { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要设置 ErrorMessage 属性,例如:
You need to set the ErrorMessage property, so for example:
大家好。
还在寻找解决方案吗?
我今天已经解决了同样的问题。您必须创建自定义验证属性来验证 2 个日期(下面的示例)。然后您需要适配器(验证器),它将使用您的自定义属性验证模型。最后一件事是将适配器与属性绑定。也许一些例子会比我更好地解释它:)
我们开始吧:
DateCompareAttribute.cs:
DateCompareAttributeAdapter.cs:
Global.asax:
CustomViewModel.cs:
hello everybody.
Still looking for solution?
I've solved the same problem today. You have to create custom validation attribute which will validate 2 dates (example below). Then you need Adapter (validator) which will validate model with your custom attribute. And the last thing is binding adapter with attribute. Maybe some example will explain it better than me :)
Here we go:
DateCompareAttribute.cs:
DateCompareAttributeAdapter.cs:
Global.asax:
CustomViewModel.cs:
我不知道有什么简单的方法可以解决此行为。这就是我讨厌数据注释的原因之一。使用 FluentValidation 进行同样的操作将是小菜一碟:
FluentValidation 具有出色的与 ASP.NET MVC 的支持和集成。
I am not aware of an easy way fix this behavior. That's one of the reasons why I hate data annotations. Doing the same with FluentValidation would be a peace of cake:
FluentValidation has great support and integration with ASP.NET MVC.
返回验证结果时使用两个参数的构造函数。
向其传递一个数组,其中 context.MemberName 作为唯一值。
希望这有帮助
When returning the validation result use the two parameter constructor.
Pass it an array with the context.MemberName as the only value.
Hope this helps