I am using Asp.net MVC 3 and Twitter Bootstrap. What I want is to integrate both of them. The big problem for me is forms. I am using the HtmlHelper
and it is a problem, when it comes to the validation, I want it to generate HTML like this:
<div class="control-group error">
<label for="field" class="control-label">OMG this label is so awesome: </label>
<div class="controls">
<input type="text" name="field" id="field" value="yeah" />
<span class="help-inline">Eventually some error :C</span>
</div>
</div>
Here is my HtmlHelper code:
@Html.LabelFor(x => x.Field)
@Html.EditorFor(x => x.Field)
@Html.ValidationMessagesFor(x => x.Field)
The problem is that I want the class error on outermost div to be set only if there actually is an error on this field. Other problem is that I don't know how to enforce using span tag for errors. I could write my method in HtmlHelper, but it'd make me reimplement almost all of the functionality of the LabelFor
, EditorFor
and ValidationMessageFor
. Is there a simpler way to do this? And what about the unobtrusive validation?
You can integrate MVC3 validation with Bootstrap framework by adding the following javascript to your page (View)
Besides, on the Views (for example "Create.cshtml") make sure that the fields in the form are formatted as the following...
A bit late answer, but I'll propose a better solution and you should totally accept my answer ;)
Use TwitterBootstrapMVC.
With a single line of code it will generate exactly the html you need:
For Global error all you need to write is:
... it will generate an alert div with all errors listed in it. Notice that for client side validation it needs some JavaScript for things to be styled properly.
On top of that it will take care of all the unobtrusive validation tags for you. It also offers fluent syntax, that enables full customization of the inputs/labels...
Check it out!
Disclaimer: I'm the author of TwitterBootstrapMVC As of Bootstrap 3 (and TwitterBootstrapMVC 3) TwitterBootstrapMVC requires a licence for usage.
There's an article on BrainDonor that I've implemented with success just using the main LabelFor, EditorFor, and ValidationFor. I've placed this is a site wide js, then just make sure I have the control-group div and it's good to go.
I took the liberty and created an extension method that encapsulates the code from Sherbrow to render the error class when the field is not valid. This extension method has the advantage that it is strongly-typed and shorter to write:
I encountered the same challenge and with some help for the labels (see below), here is what I got :
Html.LabelFor
implementation : https://stackoverflow.com/a/6722082I didn't try with the unobstrusive validation, but it seems you just have to activate it.
If you are looking for a global error, you should use something like :
There is a live example (in french) here : http://ws.sherbrow.fr/auth
The whole project source code should be available some time soon (see my profile - or ask me directly).
I don't know if this will help anyone, but I've modified M$'s jquery.validate.unobtrusive.js to achieve this. It was overriding the default settings provided by $.validator.setDefaults() and was creating problems to me. If you replace the JS file with this one it should work as you want it to work with bootstrap's CSS (no other changes needed - except some CSS classes on input elements and labels).
The $jQval.setDefaults() is the main stuff. The rest is just me fixing M$'s code.
Here is the file: