javascript - How to change the default ErrorMessage for StringLength attribute in view model with unobtrusive validation -



javascript - How to change the default ErrorMessage for StringLength attribute in view model with unobtrusive validation -

the default stringlength validation error message long purposes, create shorter.

i know can specify errormessage each property, rather not duplicate on place:

[stringlength(25, errormessage = "max length {1}")] [displayname("address")] public virtual string billaddress1 { get; set; }

based on post, modify default errormessage stringlength validation, subclassed stringlengthattribute:

public class customstringlengthattribute : stringlengthattribute { public customstringlengthattribute() : this(20) { } public customstringlengthattribute(int maximumlength) : base(maximumlength) { base.errormessage = "max length {1}"; } }

and changed utilize customstringlengthattribute instead:

[customstringlength(25)] [displayname("address")] public virtual string billaddress1 { get; set; }

this compiles fine, , set breakpoint in customstringlengthattribute constructor verify gets hit, unobtrusive validation no longer works - invalid info gets posted controller.

it works fine when not utilize subclassed version.

also, here javascript validation:

if (validation.validateform($('#addressform'))) { .... }

validateform:

function validateform(form) { preparevalidationscripts(form); var validator = $.data(form[0], 'validator'); homecoming validator.form(); } function preparevalidationscripts(form) { if (form.executed) return; form.removedata("validator"); $.validator.unobtrusive.parse(document); form.executed = true; }

what missing?

thanks.

try having custom validation.

customstringlengthattribute.cs

public class customstringlengthattribute : validationattribute, iclientvalidatable { public int maximumlength { get; private set; } public int minimumlength { get; set; } public customstringlengthattribute(int maximumlength) : base(new func<string>(customstringlengthattribute.getdefaulterrormessage)) { this.maximumlength = maximumlength; } private static string getdefaulterrormessage() { homecoming "max length {1}"; } public override string formaterrormessage(string name) { homecoming string.format(base.errormessagestring, name, this.maximumlength, this.minimumlength); } public override bool isvalid(object value) { if (value == null) { homecoming true; } string text = (string)value; int maximumlength = this.maximumlength; int minimumlength = this.minimumlength; if (maximumlength < 0) { throw new invalidoperationexception("the maximum length must nonnegative integer."); } if (minimumlength > maximumlength) { throw new invalidoperationexception(string.format("the maximum value '{0}' must greater or equal minimum value '{1}'.", maximumlength, minimumlength)); } int length = text.length; homecoming length <= maximumlength && length >= minimumlength; } public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { var rule = new modelclientvalidationrule(); rule.errormessage = formaterrormessage(metadata.getdisplayname()); rule.validationparameters.add("max", maximumlength); rule.validationparameters.add("min", minimumlength); rule.validationtype = "customstringlength"; yield homecoming rule; } }

in js file:

/// <reference path="jquery-1.4.4.js" /> /// <reference path="jquery.validate.js" /> /// <reference path="jquery.validate.unobtrusive.js" /> if ($.validator && $.validator.unobtrusive) { $.validator.unobtrusive.adapters.addminmax("customstringlength", "min","max"); $.validator.addmethod("customstringlength", function (value, element, min,max) { if (value) { if (value.length<min || value.length>max) { homecoming false; } } homecoming true; }); }

model class:

[customstringlength(25)] [displayname("address")] public virtual string billaddress1 { get; set; }

javascript jquery asp.net-mvc validation

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -