Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC2 Model Validation Fails with Non-US Date Format

I have a small MVC2 app that displays in two cultures: en-US and es-MX. One portion contains a user input for a date that is pre-populated with the current date in the Model.

When using en-US, the date field is displayed as MM/dd/yyyy and can be changed using the same format without causing any validation errors.

When using es-MX, the date field is displayed as dd/MM/yyyy, but when the date is edited in this format, the server-side validation fails with the message:

The value '17/05/1991' is not valid for The Date.

One of the first things that jumps out at me about that message is that it is not localized. Both the message itself (which I do not think I can control) and the Display Name of the field (which I can control and is localized in my code). Should be displaying in a localized format.

I have tried stepping through the code to see exactly where the validation is failing, but it seems to be happening inside some of the compiled MVC or DataAnnotations code that I cannot see.

Application details: IIS6, ASP.NET 3.5 (C#), MVC 2 RTM

Sample Model Code:

public class TestVieModel{
    [LocalizedDisplayNameDisplayName("TheDateDisplayName", NameResourceType=typeof(Resources.Model.TestViewModel))]
    [Required(ErrorMessageResourceName="TheDateValidationMessageRequired", ErrorMessageResourceType=typeof(Resources.Model.TestViewModel))]
    [DataType(DataType.Date)]
    public DateTime TheDate { get; set; }
}

Sample Controller Action Code:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(TestViewModel model) {
    if(ModelState.IsValid) {  // <--- Always is false when using es-MX and a date foramtted as dd/MM/yyyy.
        // Do other stuff
        return this.View("Complete", model);
    }

    // Validation failed, redisplay the form.
    return this.View("Enter", model);
}

Sample View Code:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HispanicSweeps.Web.Model.LosMets.EnterViewModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Test</title>
    </head>
    <body>
        <% using (Html.BeginForm()) {%>
            <%= Html.ValidationSummary(true) %>

            <fieldset>
                <legend>Fields</legend>
                <div class="editor-label">
                    <%= Html.LabelFor(model => model.TheDate) %>
                </div>
                <div class="editor-field">
                    <%= Html.EditorFor(model => model.TheDate) %>
                    <%= Html.ValidationMessageFor(model => model.TheDate) %>
                </div>

                <p><input type="submit" value="Save" /></p>
            </fieldset>

        <% } %>
    </body>
</html>
like image 277
Raleigh Buckner Avatar asked May 17 '10 18:05

Raleigh Buckner


2 Answers

Here's how I solved the issue in my case. I manually validated the date in the controller and reset the ModelState for that property:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(TestViewModel model) {

    var tempDate = new DateTime();
    var culture = CultureInfo.CurrentUICulture;

    if(DateTime.TryParse(Request.Form["TheDate"], culture, DateTimeStyles.None, out tempDate)) {
        model.DateOfBirth = tempDate;
        ModelState.Remove("TheDate");
        }

    if(ModelState.IsValid) {  // <--- Now valid
        // Do other stuff
        return this.View("Complete", model);
        }

    // Validation failed, redisplay the form.
    return this.View("Enter", model);
    }
like image 139
Raleigh Buckner Avatar answered Nov 15 '22 07:11

Raleigh Buckner


The validation occurs within the DataAnnotations class. You can subclass DataAnnotations classes for your own purposes.

I would create a new MultiCultureDateType DataAnnotations class that will validate dates across multiple cultures.

More information: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

like image 44
Dave Swersky Avatar answered Nov 15 '22 08:11

Dave Swersky