c# - ASP.NET MVC 3 Client Validation -
i have following viewmodel:
public class viewmodel { [display(name = "firstname", resourcetype = typeof(views.validation))] public string firstname { get; set; } [required(errormessageresourcename="required", errormessageresourcetype = typeof(views.validation))] [display(name="lastname", resourcetype = typeof(views.validation))] public string lastname { get; set; } ... }
and html view:
... <div class="row valid showmsg"> <div class="itemwrap clearfix"> <label>@html.labelfor(model => model.firstname)<span class="iconreq"> </span>:</label> @html.editorfor(model => model.firstname) </div> <div class="info"> <p class="errormsg">@html.validationmessagefor(model => model.firstname)</p> <p class="infomsg">info message here</p> <p class="focusmsg">text on active</p> </div> </div> ...
if notice in html view have <div class="row valid showmsg">
class "showmsg" controls display of messages inside <div class="info">
.
now, server validation wrote custom html helper adds that class "showmsg" div when not valid so:
public static mvchtmlstring validationrowfor<tmodel, tproperty>(this htmlhelper<tmodel> html, expression<func<tmodel, tproperty>> expression) { mvchtmlstring normal = html.validationmessagefor(expression); if (normal != null) { return mvchtmlstring.create("erroron"); } return null; }
and use so:
<div class="row valid @html.validationrowfor(model => model.firstname)">
i same client validation. automatically add "showmsg" class parent when error. how it?
thanks.
edit: ok works regular html not in mvc3 ??
$(function(){ var validator = $(".form").validate({ highlight: function(element) { $(element).parents().closest('div.row').addclass('erroron'); }, unhighlight: function(element) { $(element).parents().closest('div.row').removeclass('erroron'); } }); });
maybe among lines should job:
$(function () { $('form').submit(function () { $(this).validate().invalidelements().each(function () { $(this).closest('div.row.valid').addclass('showmsg'); }); }); });
Comments
Post a Comment