Skip to content

Instantly share code, notes, and snippets.

@pawelel
Last active October 16, 2021 11:56
Show Gist options
  • Save pawelel/280a39f57a76dce390c5af0e596a7f22 to your computer and use it in GitHub Desktop.
Save pawelel/280a39f57a76dce390c5af0e596a7f22 to your computer and use it in GitHub Desktop.
Custom Range attribute for System.ComponentModel.DataAnnotations
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class RangeUntilCurrentYearAttribute : RangeAttribute
{
public RangeUntilCurrentYearAttribute(int minimum) : base(minimum, DateTime.Now.Year)
{
}
}
// And use it like this:
//public class Foo
// {
// [RangeUntilCurrentYear(1900, ErrorMessage = "Please enter a valid year")]
// public int Year { get; set; }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment