I created the following custom ValidationAttribute attribute:
public class DateRangeAttribute : ValidationAttribute, IClientValidatable { public DateTime MinimumDate = new DateTime(1901, 1, 1); public DateTime MaximumDate = new DateTime(2099, 12, 31); public DateRangeAttribute(string minDate, string maxDate, string errorMessage) { MinimumDate = DateTime.Parse(minDate); MaximumDate = DateTime.Parse(maxDate); ErrorMessage = string.Format(errorMessage, MinimumDate.ToString("MM/dd/yyyy"), MaximumDate.ToString("MM/dd/yyyy")); } }
which I would like to use in my MVC4 view model as follows:
[DateRange(Resources.MinimumDate, Resources.MaximumDate, "Please enter a date between {0} and {1}")]
Resources is a class of generated resources based on a set of parameters stored in an SQL database. A simplified version of the generated code for the above two resource properties:
public class Resources { public const string MinimumDate = "PropMinimumDate"; public static string PropMinimumDate { get { return "12/15/2010" } } public const string MaximumDate = "PropMaximumDate"; public static string PropMaximumDate { get { return "12/15/2012" } } }
While I do not understand how this works, I understand that the typical use of resources in ValidationAttributes automatically maps Resources.MinimumDate to PropMinimumDate and returns the value "12/15/2010".
I cannot figure out how to manually complete this program leap so that I can pass two date values โโto my custom ValidatorAttribute attribute. As at present, "PropMinimumDate" and "PropMaximumDate" are the values โโof the minDate and maxDate (respectively) parameter passed to the DateRangeAttribute constructor.
If i try
[DateRange(Resources.PropMinimumDate, Resources.PropMaximumDate, "Please enter a date between {0} and {1}")]
I get a compilation error:
The attribute argument must be a constant expression, a typeof expression, or an array creation attribute type attribute expression
Is there a way to accomplish this task, or am I trying to do the impossible?