ASP.NET MVC Unobtrusive client validation not working

In my ASP.NET MVC 4 application, I am trying to use unobtrusive client validation using Fluent Validation.

<script src="/Scripts/jquery.validate.min.js" type="text/javascript">
</script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">
</script>

I have two .js files that VS2010 provides when creating a new ASP.NET MVC 4 application. I also included client-side validation in my web.config file.

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

As far as I know, when client validation and unobtrusive JavaScript are enabled, the input fields with the client validation rule contain the data-val = "true" attribute to initiate unobtrusive client validation. And I have this field in my input fields.

For instance,

<input class="input-validation-error" data-val="true" data-val- 
required="error text here" id="purchasePrice" 
name="PurchasePrice" type="text" value="">

<span class="field-validation-error error" data-valmsg-for="PurchasePrice" 
data-valmsg-replace="true">'Purchase Price' must not be empty.</span>

However, when I submit my form, it is sent to the controller, and my model is checked on my control code instead of the client side.

EDIT:

This is my form opening tag.

@using (Html.BeginForm("Create", "Product", FormMethod.Post, 
   new { enctype = "multipart/form-data", @class = "mainForm",
         @id = "productCreateForm" }))

Any ideas? Thank.

+5
1

MVC?

protected void Application_Start() {
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    // this line is required for fluent validation
    FluentValidationModelValidatorProvider.Configure();
}

/ :

[Validator(typeof(PersonValidator))]
public class Person {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
        RuleFor(x => x.Id).NotNull();
        RuleFor(x => x.Name).Length(0, 10);
        RuleFor(x => x.Email).EmailAddress();
        RuleFor(x => x.Age).InclusiveBetween(18, 60);
    }
}

, , ? . , :

// when validator rules are always server side
public class ServerSideValidator : AbstractValidator<Person> {
    public ServerSideValidator() {
        When(x => x.Name == "Foo", () => {
            RuleFor(x => x.Email).EmailAddress();
        });
    }
}
+3

All Articles