Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8693613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:35:05+00:00 2026-06-13T00:35:05+00:00

I have created the following custom ValidationAttribute: public class DateRangeAttribute : ValidationAttribute, IClientValidatable {

  • 0

I have created the following custom ValidationAttribute:

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"));
   }
}

that 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 generated resources class based on a set of options stored in a SQL database. A simplified version of the generated code for the above two resource properties is:

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 it works, I do understand that typical usage of resources in ValidationAttributes will automatically map Resources.MinimumDate to PropMinimumDate and return the value “12/15/2010”.

What I am cannot figure out is how to manually make that programmatic leap myself so I can pass in the two date values into my custom ValidatorAttribute. As presently coded, “PropMinimumDate” and “PropMaximumDate” are the minDate and maxDate parameter values (respectively) passed into the constructor of the DateRangeAttribute.

If I try

[DateRange(Resources.PropMinimumDate, Resources.PropMaximumDate, "Please enter a date between {0} and {1}")]

I receive the compile error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Is there a way to accomplish this task, or am I attempting the impossible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:35:06+00:00Added an answer on June 13, 2026 at 12:35 am

    You need to take the Type of the resource class as an argument and then use reflections to get the property value.

    public class DateRangeAttribute : ValidationAttribute, IClientValidatable {
      public DateTime MinimumDate = new DateTime(1901, 1, 1);
      public DateTime MaximumDate = new DateTime(2099, 12, 31);
      private Type _resourceType;
    
       public DateRangeAttribute(string minDate, string maxDate, string errorMessage, Type resourceType) {
         _resourceType = resourceType;
         var minDateProp = _resourceType.GetProperty(minDate, 
                                 BindingFlags.Static | BindingFlags.Public);
         var minDateValue = (string) minDateProp.GetValue((object) null, (object[]) null));
         MinimumDate = DateTime.Parse(minDateValue);
    
         // similarly get the value for MaxDate
    
         ErrorMessage = string.Format(errorMessage, 
                MinimumDate.ToString("MM/dd/yyyy"), MaximumDate.ToString("MM/dd/yyyy"));         
       }
    }
    

    Eg

    [DateRange(Resources.MinimumDate, Resources.MaximumDate, "Please enter a date between {0} and {1}", typeof(Resources))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom performance counter using the following code: public class PerfCounter
I have created the following custom tooltip with the custom template. <ToolTip x:Class=FireFly.Controls.CustomToolTip xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I have created a Custom preference which has the following constructor public CoordinatesPreference(Context context,
I have created a custom form class and template for my form by following
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I created a custom module for Orchard following this wonderful guide . I have
I have created the following class class Contact def initialize(id, name, phone) @id =
I have created the following project structure for my new asp.net mvc project any
I have created Custom Annotation with following: -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKPinAnnotationView *view =
I have created the following custom attribute to assist me with validating a required

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.