1
Vote

StringLength.MinLength not enforced client side

description

Hi, I am having some hard time getting StringLength to be enforced client side. I guess some javascript is out of sync?
 
I am using Visual Studio 2010, ASP.NET MVC 2.0 (final), created a new MVC web application, created a new user model :
 
public class UserModel
{
    [Required]
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
    public string EmailAddress { get; set; }
 
    [Required]
    [StringLength(200, MinimumLength = 6)]
    public string Password { get; set; }
}
 
An action set :
 
    [HttpGet]
    public ViewResult Create()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Create(UserModel user)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }
        else
        {
            return Redirect("http://www.bing.com");
        }
    }
 
Also created a view for this :
 
<asp:Content ContentPlaceHolderID="AdditionalHtmlHeaderContent" runat="server">
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.3.2.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.jquery.validate.js")%>"></script>
 
<%= Html.ClientSideValidation<ConsoleApplication11.Models.UserModel>("user") %>
 
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
<h2>ViewPage1</h2>
 
<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>
 
    <fieldset>
        <legend>Fields</legend>

        <p>
            Email address : <%: Html.TextBox("user.EmailAddress") %>
                            <%: Html.ValidationMessage("user.EmailAddress")%>
        </p>
        <p>
            Password : <%: Html.Password("user.Password") %>
                       <%: Html.ValidationMessage("user.Password")%>
        </p>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
 
<% } %>
 
<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
 
</asp:Content>
 
Then I referenced the xVal assembly and added
 
    <add namespace="xVal.Html"/>
 
in my master Web.config
 
Uppon running, in all browsers, all rules are enforced on the client side too, except the StringLength one..
 
Environment :
 
OS: Windows 7 Enterprise (final) - Microsoft Windows [Version 6.1.7600]
 
Browsers tested :
  • Internet Explorer 8 (8.0.7600.16385) : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; Creative AutoUpdate v1.40.01)
  • Opera 10.53 : Opera/9.80 (Windows NT 6.1; U; en) Presto/2.5.24 Version/10.53
     
    .NET Framework Version : 4.0
     
    jQuery versions used :
  • 1.3.6
  • 1.4.1
     
    I am attaching the test project

file attachments

comments

andreir wrote May 27, 2010 at 7:42 PM

I have found the problem.

The xVal assembly was compiled against the .NET 3.5 and the StringLengthAttribute class DOES NOT have a MinimumLength property at all. That's why in the DataAnnotationsRuleProvider.cs (xVal\RuleProviders\DataAnnotationsRuleProvider.cs) at line 25 it reads :

ruleEmitters.AddSingle<StringLengthAttribute>(x => new StringLengthRule(null, x.MaximumLength));

see the null? That's the minimum length parameter.
I've targeted the 4.0 .NET Framework, corrected the line, compiled and VOILA! it works..

It was not Steve's fault. At least not Sanderson ;)

wrote May 27, 2010 at 7:43 PM

wrote Feb 14, 2013 at 12:14 AM