自定义验证属性ASP.NET MVC [英] Custom Validation Attribute ASP.NET MVC

查看:111
本文介绍了自定义验证属性ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC中,只有在不同的CustomValidationAttribute验证了另一个字段的情况下,一个字段上的CustomValidationAttribute才可能执行.

Is it possible in ASP.NET MVC for a CustomValidationAttribute on one field to execute only if a different CustomValidationAttribute validates a different field.

我的视图需要包含单独的日期和时间字段.我有两个单独的自定义验证属性.但是是否有可能仅在日期验证属性验证为true时才检查时间验证属性?

My view needs to contain separate date and time fields. I have separate custom validation attributes for both. But is it possible that the time validation attribute is checked only when date validation attribute validates to true ?

谢谢.

推荐答案

时间验证属性已选中 仅当日期验证属性时 验证为true吗?

time validation attribute is checked only when date validation attribute validates to true ?

此语句表示自定义验证.是的,您可以这样做.您可以定义以其他字段的名称为参数的自定义验证属性.然后,在重写的Validate()方法中,您可以通过名称获取该其他字段的PropertyInfo,然后获取验证属性并对其进行验证.得到结果后,您可以决定是否在第一个字段上进行验证. 布拉德·威尔逊在mvcConf上有精彩的帖子关于验证

This statement means custom validation. Yes, you can do this. You Could Define custom validation attribute that takes other field's name as parameter. Then, in overrided Validate() method, you could get PropertyInfo for that other field by the name, then get validation attributes and validate them. After getting the result, you could decide whether to do validation on first field or not. Brad Wilson had great post on mvcConf about validation

顺便说一句,您还可以实现IClientValidatable来包装客户端验证

By the way, you could also implement IClientValidatable to wrap up client side validation

这是非常非常简单的代码,它需要一些参数检查和错误处理等.但是我认为想法很明确

This is the very very sample code and it needs some argument checking and error handling, etc. But i think idea is clear

 public class OtherFieldDependentCustomValidationAttribute : ValidationAttribute
{
    public readonly string _fieldName;

    public OtherFieldDependentCustomValidationAttribute(string otherFieldName)
    {
        _fieldName = otherFieldName;
    }

    protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext)
    {
        //Get PropertyInfo For The Other Field
        PropertyInfo otherProperty = validationContext.ObjectType.GetProperty(_fieldName);

        //Get ValidationAttribute of that property. the OtherFieldDependentCustomValidationAttribute is sample, it can be replaced by other validation attribute
        OtherFieldDependentCustomValidationAttribute attribute = (OtherFieldDependentCustomValidationAttribute)(otherProperty.GetCustomAttributes(typeof(OtherFieldDependentCustomValidationAttribute), false))[0];

        if (attribute.IsValid(otherProperty.GetValue(validationContext.ObjectInstance, null), validationContext) == ValidationResult.Success)
        {
            //Other Field Is valid, do some custom validation on current field

            //custom validation....

            throw new ValidationException("Other is valid, but this is not");
        }
        else
        {
            //Other Field Is Invalid, do not validate current one
            return ValidationResult.Success;
        }
    }
}

这篇关于自定义验证属性ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆