6

In an Asp.net MVC app, I have inherited this problem (if it is a problem?) where one developer has used String for Date type.

In my model the property reads:

[Required]
[DisplayName("Registration Date")]
public string Registrationdate { get; set; }

The business requirement is that the field is not required, but if there is something in that fields then it must be a valid date.

How would you implement this requirement, without changing the data type?

1
  • 1
    Would probably do something along the lines of implementing a check using DateTime.TryParse in the setter method Commented Feb 2, 2011 at 22:25

4 Answers 4

9

It looks like you're using System.ComponentModel.DataAnnotations. The best way to do this using this library would be to create a new attribute to validate date strings and apply it to the property. Here's some code for you to start with:

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
class DateAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var dateString = value as string;
        if (string.IsNullOrWhiteSpace(dateString))
        {
            return true; // Not our problem
        }
        DateTime result;
        var success = DateTime.TryParse(dateString, out result);
        return success;
    }
}

You'll probably want to expand on this code depending on what kind of strings you're expecting from the client. Also, this won't give you any client-side validation.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: you are absolutely right that I am using DataAnnotations and was hoping for something similar to this type of solution. I am going to try this out this evening.
6
public string Registrationdate { 
    get; 
    set {
        DateTime date;
        var isDate = DateTime.TryParse(value, out date);
        if (isDate) { 
            _registrationDate = value; 
        }
        else {
          // Throw exception
        }
    }
}

1 Comment

Ok thanks, this is not a bad approach. But how do I show this as a validation errors in a MVC app? +1
1

(sort of) pseudocode:

if (Registrationdate is not empty)
{
    RegistrationDateTime = new DateTime(Registrationdate);

    if (RegistrationDateTime is not valid DateTime)
        fail validation;
}

Comments

0

How about a regular expression? Data Annotations has a regex attribute. Now you'd have to fix on a format, say ISO (yyyy/mm/dd) which may not meet your requirements.

Another alternative may be to create your own annotation.

Yet another solution could to use a nullable datetime (DateTime?). I'm not sure how that would be handled though, so some trial and error would be needed. It does however only need the addition of one ? so could be relatively easy to try.

Simon

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.