MVC3 / EF CustomValidator 模型中的两个字段
使用 MVC3 和 EF4.1 如何在客户端和服务器上验证视图模型中的多个字段?
我有一个开始日期文本框(可以修改),并且原始开始日期位于隐藏字段中。当用户提交表单时,我想检查修改后的开始日期是否不超过原始开始日期两侧的一个月。
我不知道如何使用 DataAnnotation 和 CustomValidation 来完成此操作(或者也许我走错了路)?这是我一直在使用的示例:
[MetadataType(typeof(Metadata.MyUserMetaData))]
public partial class MyUser
{
public System.DateTime DateOfBirth { get; set; }
}
部分类
public class MyUserMetaData
{
[CustomValidation(typeof(AmendedStartDate), "amendedstartdate", ErrorMessage = "Invalid date."]
public DateTime StartDate { get; set; };
public DateTime OriginalStartDate { get; set; };
}
自定义验证器
public class AmendedStartDate : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// How do I get multiple field values from object value?
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(Modelmetadata metadate, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "amendedstartdate"
};
yield return rule;
}
}
我知道我仍然需要将 jQuery 添加到此验证器的视图中。
Using MVC3 and EF4.1 how do I validate on client and server more than one field in my view model?
I have a start date text box (that can be modified) and I have the original start date in a hidden field. When the user submits the form I want to check that the modied start date is no more than one month either side of the original start date.
I can't figure out how this can be done with DataAnnotation and CustomValidation (or maybe I'm going down the wrong road)? This is an example of whay I've been working with:
[MetadataType(typeof(Metadata.MyUserMetaData))]
public partial class MyUser
{
public System.DateTime DateOfBirth { get; set; }
}
Partial Class
public class MyUserMetaData
{
[CustomValidation(typeof(AmendedStartDate), "amendedstartdate", ErrorMessage = "Invalid date."]
public DateTime StartDate { get; set; };
public DateTime OriginalStartDate { get; set; };
}
Custom Validator
public class AmendedStartDate : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// How do I get multiple field values from object value?
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(Modelmetadata metadate, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "amendedstartdate"
};
yield return rule;
}
}
I know I've still to add jQuery to the view for this validator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在模型类上实现
IValidatableObject
而不是使用数据注释 - 在交叉验证的场景中它更简单、更清晰。如果您仍想使用
ValidationAttribute
,则IsValid
方法中有两个参数:value
表示分配属性的属性的经过验证的值ObjectInstance
和ObjectType
属性,以便您可以转换实例并访问其他属性。Instead of using data annotations implement
IValidatableObject
on your model class - it is simpler and much more clear in scenarios with cross validation.If you still want to use
ValidationAttribute
you have two parameters in theIsValid
method:value
represents validated value of the property where the attribute is assignedcontext
is context in which the property is validated. It also containsObjectInstance
andObjectType
properties to access the whole model and its type so you can cast the instance and access other properties.MVC 自定义验证:比较两个日期中提出的问题有一个示例与模型中的第二个值进行比较的验证器。这应该可以帮助你开始。
The question asked in MVC custom validation: compare two dates has an example of a validator which compares to a second value in the model. That should get you started.