Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach for extending unobtrusive javascript in MVC3 to add a style to a div client side

I'm using html5/Razor/MVC3 leveraging the Bootstrap template from Twitter. I want to have form validation that looks slick like they've documented (http://twitter.github.com/bootstrap/#forms). So if we take a look at how the standard boiler-plate MVC3 for account registration, the markup would look like:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class="form-stacked" })) {     @Html.ValidationSummary(true, "Snap! Something went wrong")     <div>         <fieldset>             <legend>Account Information</legend>             <div class="clearfix error">                 @Html.LabelFor(m => m.UserName)                 <div class="input">                     @Html.TextBoxFor(m => m.UserName)                     <span class="help-inline">@Html.ValidationMessageFor(m => m.UserName)</span>                 </div>             </div>              <div class="clearfix">                 @Html.LabelFor(m => m.Email)                 <div class="input">                     @Html.TextBoxFor(m => m.Email)                     <span class="help-inline">@Html.ValidationMessageFor(m => m.Email)</span>                 </div>             </div>              <div class="clearfix">                 @Html.LabelFor(m => m.Password)                 <div class="input">                     @Html.PasswordFor(m => m.Password)                     <span class="help-inline">@Html.ValidationMessageFor(m => m.Password)</span>                 </div>             </div>              <div class="clearfix">                 @Html.LabelFor(m => m.ConfirmPassword)                 <div class="input">                     @Html.PasswordFor(m => m.ConfirmPassword)                     <span class="help-inline">@Html.ValidationMessageFor(m => m.ConfirmPassword)</span>                 </div>             </div>         </fieldset>         <div class="actions">             <button class="btn large primary" type="submit">Register</button>         </div>     </div> 

What I want to do is have the container div inject the "error" class like I've hard-coded in the first input. (So upon entering the page, the div would have a class of "clearfix" but if that input block failed validation, it would tag it as "clearfix error"). I figure I'm going to have to update the div block to include an id of some sort and perhaps add a new data- attribute to the ValidationMessage. I don't have a problem extending the ValidationMessageFor helper. I'm just not 100% sure what the approach should be for extending the library that's there. Any suggestions on how to approach this?

TIA.

UPDATE:

I am thinking this approach is reasonable:

<div id="UserNameContainer" class="clearfix error">     @Html.LabelFor(m => m.UserName)     <div class="input">         @Html.TextBoxFor(m => m.UserName)         <span class="help-inline">@Html.ValidationMessageFor(m => m.UserName, null, new { @data_container = "UserNameContainer" })</span>     </div> </div> 

By decorating my validation message with a data-container name, I could then target the container div. Now I just need to figure out how to intercept the validation message.

like image 372
JBright Avatar asked Aug 27 '11 15:08

JBright


People also ask

What is use of unobtrusive JavaScript in MVC?

Unobtrusive Validation allows us to take the already-existing validation attributes and use them client-side to make our user experience that much nicer. The Unobtrusive script files are included automatically with new MVC projects in Visual Studio, but if you don't have them you can get them from NuGet.

How is unobtrusive validation implemented in ASP net MVC?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.

Which of the following are the benefits of using unobtrusive JavaScript?

There are several benefits of using Unobtrusive JavaScript. Separation of concerns i.e the HTML markup is now clean without any traces of javascript. Page load time is better. It is also easy to update the code as all the Javascript logic is present in a separate file.


1 Answers

The $.validator.setDefaults method solved this issue for me with Bootstrap from Twitter. I'm usingjquery.validate.js and jquery.validate.unobtrusive.js.

Since unobtrusive validation on DOM ready scans your document and caches unobtrusive validation options for each form it encounters, it is needed to call the $.validator.setDefaults method before document scan occurs.

// setup defaults for $.validator outside domReady handler $.validator.setDefaults({     highlight: function (element) {         $(element).closest(".clearfix").addClass("error");     },     unhighlight: function (element) {         $(element).closest(".clearfix").removeClass("error");     } });  $(document).ready(function() {        // do other stuff }); 
like image 161
Germán Avatar answered Sep 21 '22 02:09

Germán