jquery - MVC4 & IClientValidatable - Automatic AJAX calls to server side validation -
i'm looking implement custom client side validation in mvc4. have working standard attributes, such in model
public class uploadedfiles { [stringlength(255, errormessage = "path long.")] [required(errormessage = "path cannot empty.")] [validpath] public string sourcedirectory { get; set; } }
so stringlength , required both automatically translated jquery client side validation. "valid path" works server side. validation required server side server can validate if path valid, couldn't client side.
the server side code looks like
public class validpathattribute : validationattribute, iclientvalidatable { public string sourcedirectory; protected override validationresult isvalid(object value, validationcontext validationcontext) { string path = value.tostring(); string message = string.empty; var filesystemsupport = new filesystemsupport(settings, new wrappedfilesystem(new filesystem())); if (filesystemsupport.validatenetworkpath(path, out message)) { return validationresult.success; } return new validationresult(message); } }
and works fine. happen via ajax call, step in "iclientvalidatable" , "getclientvalidationrules". following book have written
public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { var rule = new modelclientvalidationrule(); rule.errormessage = formaterrormessage(metadata.getdisplayname()); rule.validationtype = "validpath"; yield return rule; }
i believe have write custom validation script code, adapter (to identify required metadata) , validation rule (the validator, referenced rule.validationtype).
i don't think need write adapter, can use
addbool - create adapter validator rule "on" or "off". rule requires no additional parameters
so in uploadedfiles.js have
$.validator.unobtrusive.adapters.addbool("validpath", "required");
and in view
@section scripts { @scripts.render("~/bundles/jqueryval") @scripts.render("~/scripts/uploadedfiles.js") }
i believe enough hook up, need write javascript validator. these live in jquery.validator object , can added $.validator.addmethod.
this come bit unstuck several reasons:
1) correct way things, if validation lives server side ajax call? need synchronous.
2) there element of jquery should reusing this? had hoped given i've done work server side enable magic hook client side (much standard validation).
3) reusable, across various custom validation attributes. how can make generic?
apologies if i've made mountain out of mole hill. time :)
russ
mvc comes remoteattribute
internally makes ajax call controller method returns json value indicating if validation succeeded or failed
public jsonresult isvalid(string sourcedirectory) { if (somecondition) //test if value of sourcedirectory valid { return json(true, jsonrequestbehavior.allowget); // indicates valid } else { return json(false, jsonrequestbehavior.allowget); // indicates not valid // or return json("a custom error message overrides default message defined in attribute"); } }
and decorate property
[remote("isvalid", "yourcontroller", errormessage = "the path not valid")] public string sourcedirectory { get; set; }
note: remoteattribute
client side (jquery unobtrusive validation) , may still need additional server side validation.
refer how to: implement remote validation in asp.net mvc detailed example
Comments
Post a Comment