ASP .NET MVC 3 Validation unobtrusive - 验证规则的评估顺序
我创建了一个自定义 ValidationAttribute“RequiredIf”,用于根据另一个字段确定一个字段是否为空。我已经创建了适配器并添加了方法。
这是示例: http://anthonyvscode .com/2011/07/14/mvc-3-requiredif-validator-for-multiple-values/
问题在于该属性的元数据多于requiredif 且属性为 Int32,因为如果我的表单在客户端上进行验证,则首先评估该值是否为数字,然后评估该值是否为空。
[Display(Name = "Number of cars")]
[RequiredIf("IsRent", true, ErrorMessage = "Number of cars is required")]
[Range(0, 9, ErrorMessage = "Error range")]
public virtual Int32? NumberCars
{
set;
get;
}
我该如何解决这个问题?需要正确的规则顺序if、数量和范围。
I have created a custom ValidationAttribute "RequiredIf", to determinate wether a field is empty or not according to another field. I have created the adapter and I have added the method.
Here is the example: http://anthonyvscode.com/2011/07/14/mvc-3-requiredif-validator-for-multiple-values/
The problem is that the property has more metadata than requiredif and the property is Int32, for that if my form is validated on client, firstly it is evaluated if the value is a number and after if the value is empty.
[Display(Name = "Number of cars")]
[RequiredIf("IsRent", true, ErrorMessage = "Number of cars is required")]
[Range(0, 9, ErrorMessage = "Error range")]
public virtual Int32? NumberCars
{
set;
get;
}
How can I fix it?The right order of the rules would be requiredif, number and range.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论