Data Annotation Ranges of Dates

后端 未结 7 1437
故里飘歌
故里飘歌 2020-12-08 18:53

Is it possible to use [Range] annotation for dates?

something like

[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.To         


        
7条回答
  •  旧时难觅i
    2020-12-08 18:55

    I use this approach:

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    internal sealed class DateRangeAttribute : ValidationAttribute
    {
        public DateTime Minimum { get; }
        public DateTime Maximum { get; }
    
        public DateRangeAttribute(string minimum = null, string maximum = null, string format = null)
        {
            format = format ?? @"yyyy-MM-dd'T'HH:mm:ss.FFFK"; //iso8601
    
            Minimum = minimum == null ? DateTime.MinValue : DateTime.ParseExact(minimum, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None); //0 invariantculture
            Maximum = maximum == null ? DateTime.MaxValue : DateTime.ParseExact(maximum, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None); //0 invariantculture
    
            if (Minimum > Maximum)
                throw new InvalidOperationException($"Specified max-date '{maximum}' is less than the specified min-date '{minimum}'");
        }
        //0 the sole reason for employing this custom validator instead of the mere rangevalidator is that we wanted to apply invariantculture to the parsing instead of
        //  using currentculture like the range attribute does    this is immensely important in order for us to be able to dodge nasty hiccups in production environments
    
        public override bool IsValid(object value)
        {
            if (value == null) //0 null
                return true;
    
            var s = value as string;
            if (s != null && string.IsNullOrEmpty(s)) //0 null
                return true;
    
            var min = (IComparable)Minimum;
            var max = (IComparable)Maximum;
            return min.CompareTo(value) <= 0 && max.CompareTo(value) >= 0;
        }
        //0 null values should be handled with the required attribute
    
        public override string FormatErrorMessage(string name) => string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Minimum, Maximum);
    }
    

    And use it like so:

    [DateRange("2004-12-01", "2004-12-2", "yyyy-M-d")]
    ErrorMessage = "Value for {0} must be between {1} and {2}")]
    

提交回复
热议问题