I need to validate my input in MVC4 razor. The problem is, I want to use twitter bootstrap and then set the error message class for the top div
as seen here: http://getbootstrap.com/css/#forms-control-states
How can I change the parent div
's class?
Solution
Include last:
Currently in jquery.validate.unobtrusive.js the following code shows how MS allows for extending itself:
Allows you to basically override/extend the following options:
It does this by looking at the global variable
$.validator.unobtrusive.options
(which doesn't exist unless you make it).defaultOptions
is set to$.validator.unobtrusive.options
andexecInContext
is a method that finds the applicable method in$.validator.unobtrusive.options
and executes it with the original arguments (so that the original MS method doesn't get clobbered).Have you tried this script?
http://blog.ijasoneverett.com/2013/05/asp-net-mvc-validation-with-twitters-bootstrap/
I use something similar for getting bootstrap and mvc validation to play nice
The accepted answer needed modifications to work with Bootstrap 3. I have used the following with success with MVC 4 and Bootstrap 3.
Modify validator defaults:
Create a new validation summary partial (so you have complete control over markup):
Add one style to your CSS (to hide the validation summary on initial load without needing to depend on client-side scripting to hide it after the fact):
And render your alternative validation summary in your view instead of @Html.ValidationSummary():
And you're good to go. Oh, and to get the nice styling for checkbox and radio, depending on your setup the above script will catch it, but the easiest is to simply modify your i.e. Boolean.cshtml partial to be like the following (where Html.FormGroupFor is just a slightly updated version of https://github.com/erichexter/twitter.bootstrap.mvc/blob/master/src/Bootstrap/BootstrapSupport/ControlGroupExtensions.cs - just change "control-group" references to "form-group" and add " controlGroupWrapper.AddCssClass("has-error");" on line 58'ish):
All of the above is compilation of various solutions I've found from many different locations that have made the right mix. Ben's solution above works, but it does a fair bit client-side which causes a jitter whenever some method in it is called (i.e. post, page load, validation error). The above does all that you need without having all that DOM looping.
The various answers found at Bootstrap 3 with jQuery Validation Plugin cover much of the above (and credit for a significant chunk of the above goes to the various posters there, although no one answer covers it all).
I like to add class on server side, and not client side, so, my solution has no javascript (in my case, it's a simple system with only 2 or 3 fields on the form).
This is to use the same structure from Form -> Validation States from Bootstrap documentation, available in http://getbootstrap.com/css/#forms-control-validation
First, I created a
Html Helper
to check if the ModelState is valid, and if has a error on the field, return a string (class name). I could have done this directly on the view, but I like a cleaner view. So, my class:Imports:
Now, in my View, I just need to use the class, like: