views:

2217

answers:

3

I am having issues adding validation methods to a couple of controls in my MVC app. I use the following to test for the mm/dd/yyyy format:

if (!Regex.IsMatch(candidateToEdit.availability.StartDate.ToShortDateString(), @"giantregex"))
            ModelState.AddModelError("availability_StartDate", "Start date must be in the mm/dd/yyyy format.");
//giantregex is a giant regular expression omitted for clarity

In my view I have:

<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

<%= Html.ValidationMessage("availability_StartDate", "*")%>

For whatever reason the error text is not being displayed, it acknowledges there is an error and the start of the list is generated, but the "Start date must be in the mm/dd/yyyy format." is not displayed. It validates if you put in the date correctly.

A: 

I think you need to include a validation summary to get the message

<%= Html.ValidationSummary() %>

EDIT: Try putting a "." instead of a "_" as your property name in the AddModelError call, like this:

Instead of:

ModelState.AddModelError("availability_StartDate", "Start date must be...");

try this:

ModelState.AddModelError("availability.StartDate", "Start date must be...");
Josh E
The <%= Html.ValidationSummary() %> is present. I will edit my original post to clarify that.
Graham
Changing it from a _ to a . doesn't seem to make a difference, this is strange. It's working with all of the other messages just fine.
Graham
Yeah breakpointing in my code shows . used for model error keys instead of _ (though I am using xVal but I am pretty sure the operation remains the same).
cfeduke
A: 

The sample you gave works at my testproject. Can you try to reproduce the error in a freshly created project?

Malcolm Frexner
+2  A: 

I think the problem here is you're testing an actual DateTime type against a regular expression. Because they have entered an invalid date time format in the text box, it is never actually parsed into an actual DateTime where ToShortDateString() could be invoked on it. Therefore your regular expression validation is never actually occurring.

You'll need to adopt the ViewModel pattern where you expose all potential parsing problems as strings first (such as "candidateToEditViewModel.AvailabilityStartDateString") or implement client side validation and program defensively.

cfeduke
That makes sense, it looks like when the invalid date is posted it is initialized to a default value though, which if that's the case the regular expression should return true, which it doesn't.
Graham
I think what you are seeing is the built in validation because you declared (I assume) a DateTime and not a DateTime? so the property is actually *required*.
cfeduke
So i was able to cheat a little bit based on the logic that I need. Every time an invalid date is posted a new DateTime is just initialized, so I just check to see if the StartDate value is equal to the initialized value of DateTime and throw the error if it's true. Marked as the answer since it lead me directly to the fix. Thanks.
Graham
Providing an invalid date results in an InvalidOperationException on the server when the invalid value from the client is assigned to the DateTime property and fails. For that exact reason in our ASP.NET MVC application we had to add string properties named like OriginalPropertyNameString and we assign the values passed from the client to them first. They are parsed with DateTime.Parse() and if it passes, the value is re-assigned to the DateTime property, if it fails a validation exception is thrown and added to the ModelState.
Pawel Krakowiak