更改验证范围编程(MVC3 ASP.NET) [英] Changing validation range programmatically (MVC3 ASP.NET)

查看:121
本文介绍了更改验证范围编程(MVC3 ASP.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个视图模型:

Let's say I have this view model:



    public class MyModel
    {
        [Range(0, 999, ErrorMessage = "Invalid quantity")]
        public int Quantity { get; set; }
    }

现在,对于该模型有效的值的范围将改变的具体实例:一些可能不是0,有些可能不大于5的最小/对有效范围的最大值是从DB中来,并且可以更改任何时候。

Now, for specific instances of this model the range of valid values will change: some may not be 0, some may not be higher than 5. The min/max values for the valid ranges are coming from the DB and can change anytime.

如何更改动态最小/最大的RangeAttribute的属性?或者它的最佳方式来验证我的方案?

How do I change the min/max properties of the RangeAttribute on the fly? Or what it the best way to validate my scenario?

推荐答案

沿着这个东西线可能会更你的后...

Something along the lines of this might be more what your after...

视图模型:

public class ViewModel
{
    public DateTime MinDate {get; set;}
    public DateTime MaxDate {get; set;}

    [DynamicRange("MinDate", "MaxDate", ErrorMessage = "Value must be between {0} and {1}")]
    public DateTime Date{ get; set; }
}

Library类或其它地方:

Library class or elsewhere:

public class DynamicRange : ValidationAttribute, IClientValidatable
    {
        private readonly string _minPropertyName;
        private readonly string _maxPropertyName;

    public DynamicRange(string minPropName, string maxPropName)
    {
        _minPropertyName = minPropName;
        _maxPropertyName = maxPropName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var minProperty = validationContext.ObjectType.GetProperty(_minPropertyName);
        var maxProperty = validationContext.ObjectType.GetProperty(_maxPropertyName);

        if(minProperty == null)
            return new ValidationResult(string.Format("Unknown property {0}", _minPropertyName));

        if (maxProperty == null)
            return new ValidationResult(string.Format("Unknown property {0}", _maxPropertyName));

        var minValue = (int) minProperty.GetValue(validationContext.ObjectInstance, null);
        var maxValue = (int) maxProperty.GetValue(validationContext.ObjectInstance, null);

        var currentValue = (int) value;

        if (currentValue <= minValue || currentValue >= maxValue)
        {
            return new ValidationResult(string.Format(ErrorMessage, minValue, maxValue));
        }

        return null;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
            {
                ValidationType = "dynamicrange",
                ErrorMessage = ErrorMessage
            };

        rule.ValidationParameters["minvalueproperty"] = _minPropertyName;
        rule.ValidationParameters["maxvalueproperty"] = _maxPropertyName;
        yield return rule;
    }

从:<一href=\"http://stackoverflow.com/questions/7851035/mvc-unobtrusive-range-validation-of-dynamic-values\">MVC动态值不显眼的范围验证

这篇关于更改验证范围编程(MVC3 ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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