MVC3 / EF CustomValidator 模型中的两个字段

发布于 2024-12-02 07:08:05 字数 1335 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-09 07:08:05

在模型类上实现 IValidatableObject 而不是使用数据注释 - 在交叉验证的场景中它更简单、更清晰。

如果您仍想使用 ValidationAttribute,则 IsValid 方法中有两个参数:

  • value 表示分配属性的属性的经过验证的值
  • < code>context 是验证属性的上下文。它还包含用于访问整个模型及其类型的 ObjectInstanceObjectType 属性,以便您可以转换实例并访问其他属性。

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 the IsValid method:

  • value represents validated value of the property where the attribute is assigned
  • context is context in which the property is validated. It also contains ObjectInstance and ObjectType properties to access the whole model and its type so you can cast the instance and access other properties.
抚你发端 2024-12-09 07:08:05

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文