MvcValidatorToolkit/ValidationSet.cs
/// <summary>
/// Creates the complete client-side script for the defined and translated validators.
/// </summary>
Creates the complete client-side script for the defined and translated validators.
Creates the complete client - side script for the defined and translated validators .
public string CreateClientScript(NameValueCollection errorMessages)
		{
			List<string> methods = new List<string>();
			List<string> methodsCreated = new List<string>();
			OrderedDictionary rules = new OrderedDictionary();
			OrderedDictionary messages = new OrderedDictionary();
			Dictionary<string, List<Type>> handledElements = new Dictionary<string, List<Type>>();
			List<Validator> validators = new List<Validator>();
			foreach(Validator validator in GetValidators().ToArray())
				validators.AddRange(validator.Translate());
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("function updateSettingsFor{0}(v){{", GetType().Name);
			sb.AppendLine();
			foreach(Validator validator in validators.ToArray())
			{
				InitialzeValidator(validator);
				ValidatorMethodData vmd = validator.GetClientMethodData();
				if(vmd != null)
				{
					if(string.IsNullOrEmpty(vmd.Name) || string.IsNullOrEmpty(vmd.Function) || string.IsNullOrEmpty(vmd.ErrorMessage))
						throw new ArgumentException(string.Format("The ValidatorMethodData instance returned for validator type {0} is invalid", validator.GetType()));
					if(!methodsCreated.Contains(vmd.Name))
					{
						methods.Add(string.Format("$.validator.addMethod('{0}',{1},{2});", vmd.Name.Trim("'".ToCharArray()), vmd.Function, vmd.ErrorMessage));
						methodsCreated.Add(vmd.Name);
					}
				}
				if(validator.ElementsToValidate != null)
				{
					foreach(string element in validator.ElementsToValidate)
					{
						if(handledElements.ContainsKey(element))
						{
							if(handledElements[element].Contains(validator.GetType()))
								throw new Exception(string.Format(
									"Element {0} is used in multiple instances of validator {1}",
									element, validator.GetType()));
						}
						else
						{
							handledElements.Add(element, new List<Type>());
							handledElements[element].Add(validator.GetType());
						}
						if(rules.Contains(element) == false)
							rules.Add(element, new List<string>());
						if(messages.Contains(element) == false)
							messages.Add(element, new List<string>());
						string rule = validator.GetClientRule(element);
						string message = validator.GetClientMessage(element);
						if(!string.IsNullOrEmpty(rule))
							((List<string>)rules[element]).Add(rule);
						if(!string.IsNullOrEmpty(message))
							((List<string>)messages[element]).Add(message);
					}
				}
			}
			foreach(string method in methods)
				sb.AppendFormat("\t{0}", method).AppendLine();
			sb.AppendLine("\tif(typeof(v)!='object')");
			sb.AppendLine("\t\treturn null;");
			foreach(DictionaryEntry de in rules)
			{
				string list = string.Join("," + Environment.NewLine + "\t\t\t", ((List<string>)de.Value).ToArray());
				if(!string.IsNullOrEmpty(list))
				{
					sb.AppendFormat("\tif(v.settings.rules.{0}==undefined){{", de.Key.ToString()).AppendLine();
					sb.AppendFormat("\t\tv.settings.rules.{0}={{", de.Key.ToString()).AppendLine();
					sb.AppendFormat("\t\t\t{0}", list).AppendLine();
					sb.AppendLine("\t\t};");
					sb.AppendLine("\t}");
				}
			}
			foreach(DictionaryEntry de in messages)
			{
				string list = string.Join("," + Environment.NewLine + "\t\t\t", ((List<string>)de.Value).ToArray());
				if(!string.IsNullOrEmpty(list))
				{
					sb.AppendFormat("\tif(v.settings.messages.{0}==undefined){{", de.Key.ToString()).AppendLine();
					sb.AppendFormat("\t\tv.settings.messages.{0}={{", de.Key.ToString()).AppendLine();
					sb.AppendFormat("\t\t\t{0}", list).AppendLine();
					sb.AppendLine("\t\t};");
					sb.AppendLine("\t}");
				}
			}
			if(errorMessages != null && errorMessages.Count > 0)
			{
				List<string> list = new List<string>();
				foreach(string element in errorMessages.AllKeys)
					list.Add(element + ":'" + errorMessages[element] + "'");
				sb.AppendFormat("\tv.showErrors({{{0}}});", string.Join(",", list.ToArray()));
				sb.AppendLine();
			}
			sb.AppendLine("\treturn v;");
			sb.AppendLine("}");
			return sb.ToString();
		}
public string CreateClientScript ( NameValueCollection errorMessages ) { List < string > methods = new List < string > ( ) ; List < string > methodsCreated = new List < string > ( ) ; OrderedDictionary rules = new OrderedDictionary ( ) ; OrderedDictionary messages = new OrderedDictionary ( ) ; Dictionary < string , List < Type > > handledElements = new Dictionary < string , List < Type > > ( ) ; List < Validator > validators = new List < Validator > ( ) ; foreach ( Validator validator in GetValidators ( ) . ToArray ( ) ) validators . AddRange ( validator . Translate ( ) ) ; StringBuilder sb = new StringBuilder ( ) ; sb . AppendFormat ( " " , GetType ( ) . Name ) ; sb . AppendLine ( ) ; foreach ( Validator validator in validators . ToArray ( ) ) { InitialzeValidator ( validator ) ; ValidatorMethodData vmd = validator . GetClientMethodData ( ) ; if ( vmd != null ) { if ( string . IsNullOrEmpty ( vmd . Name ) || string . IsNullOrEmpty ( vmd . Function ) || string . IsNullOrEmpty ( vmd . ErrorMessage ) ) throw new ArgumentException ( string . Format ( " " , validator . GetType ( ) ) ) ; if ( ! methodsCreated . Contains ( vmd . Name ) ) { methods . Add ( string . Format ( " " , vmd . Name . Trim ( " " . ToCharArray ( ) ) , vmd . Function , vmd . ErrorMessage ) ) ; methodsCreated . Add ( vmd . Name ) ; } } if ( validator . ElementsToValidate != null ) { foreach ( string element in validator . ElementsToValidate ) { if ( handledElements . ContainsKey ( element ) ) { if ( handledElements [ element ] . Contains ( validator . GetType ( ) ) ) throw new Exception ( string . Format ( " " , element , validator . GetType ( ) ) ) ; } else { handledElements . Add ( element , new List < Type > ( ) ) ; handledElements [ element ] . Add ( validator . GetType ( ) ) ; } if ( rules . Contains ( element ) == false ) rules . Add ( element , new List < string > ( ) ) ; if ( messages . Contains ( element ) == false ) messages . Add ( element , new List < string > ( ) ) ; string rule = validator . GetClientRule ( element ) ; string message = validator . GetClientMessage ( element ) ; if ( ! string . IsNullOrEmpty ( rule ) ) ( ( List < string > ) rules [ element ] ) . Add ( rule ) ; if ( ! string . IsNullOrEmpty ( message ) ) ( ( List < string > ) messages [ element ] ) . Add ( message ) ; } } } foreach ( string method in methods ) sb . AppendFormat ( " \t " , method ) . AppendLine ( ) ; sb . AppendLine ( " \t " ) ; sb . AppendLine ( " \t \t " ) ; foreach ( DictionaryEntry de in rules ) { string list = string . Join ( " " + Environment . NewLine + " \t \t \t " , ( ( List < string > ) de . Value ) . ToArray ( ) ) ; if ( ! string . IsNullOrEmpty ( list ) ) { sb . AppendFormat ( " \t " , de . Key . ToString ( ) ) . AppendLine ( ) ; sb . AppendFormat ( " \t \t " , de . Key . ToString ( ) ) . AppendLine ( ) ; sb . AppendFormat ( " \t \t \t " , list ) . AppendLine ( ) ; sb . AppendLine ( " \t \t " ) ; sb . AppendLine ( " \t " ) ; } } foreach ( DictionaryEntry de in messages ) { string list = string . Join ( " " + Environment . NewLine + " \t \t \t " , ( ( List < string > ) de . Value ) . ToArray ( ) ) ; if ( ! string . IsNullOrEmpty ( list ) ) { sb . AppendFormat ( " \t " , de . Key . ToString ( ) ) . AppendLine ( ) ; sb . AppendFormat ( " \t \t " , de . Key . ToString ( ) ) . AppendLine ( ) ; sb . AppendFormat ( " \t \t \t " , list ) . AppendLine ( ) ; sb . AppendLine ( " \t \t " ) ; sb . AppendLine ( " \t " ) ; } } if ( errorMessages != null && errorMessages . Count > 0 ) { List < string > list = new List < string > ( ) ; foreach ( string element in errorMessages . AllKeys ) list . Add ( element + " " + errorMessages [ element ] + " " ) ; sb . AppendFormat ( " \t " , string . Join ( " " , list . ToArray ( ) ) ) ; sb . AppendLine ( ) ; } sb . AppendLine ( " \t " ) ; sb . AppendLine ( " " ) ; return sb . ToString ( ) ; }
Creates the complete client-side script for the defined and translated validators.
Creates the complete client - side script for the defined and translated validators .
// Get defined validators and translates them into final list of validators // Collect the client rules, messages and method definitions for each validator // Get the method data if not already registered // Get the rules and message for each element the validator is validating // Avoid creating and adding client data if element is used in  // multiple validation cases // Generate client rules for the jQuery validation plugin // Generate client messages for the jQuery validation plugin // Generate client code to display server-side generated error messages
errorMessages
NameValueCollection