/// <summary> /// Validates the given element using the Values collection and generates an error if /// invalid. /// </summary> protected override void Validate(string element) { if (Values.ContainsKey(element) == false || !Values.ContainsKey(ReferenceElement) || (Values[element] ?? string.Empty) != (Values[ReferenceElement] ?? string.Empty)) { InsertError(element, ValidationSet.GetLocalizedText(ReferenceElement)); } }
/// <summary> /// Validates the HTML form data (Request.Form) using the validation set defined for the /// calling controller action. This mehtod must be called within a controller action. /// </summary> public static bool ValidateForm(this Controller controller) { // Get method data for calling method (should be a controller action) StackTrace st = new StackTrace(false); StackFrame fr = st.GetFrame(1); MethodBase mb = fr.GetMethod(); // Get the validation set attribute ValidationSetAttribute[] attributes = (ValidationSetAttribute[])mb.GetCustomAttributes(typeof(ValidationSetAttribute), false); // Stop processing if not exactly one validation set is defined if (attributes.Length == 0) { throw new Exception("The ValidateForm method expects a ValidationSetAttribute defined for the calling controller action"); } if (attributes.Length > 1) { throw new Exception("The ValidateForm method expects just one ValidationSetAttribute defined for the calling controller action"); } // Get instance of the validation set class ValidationSet vs = attributes[0].ValidationSet; return(ValidateForm(controller, vs)); }
/// <summary> /// Validates the given element using the Values collection and generates an error if /// invalid. /// </summary> public bool Validate(NameValueCollection values, ValidationSet validationSet, List <string> skipElements) { if (values == null) { throw new ArgumentNullException("values"); } if (validationSet == null) { throw new ArgumentNullException("validationSet"); } Values = values; ValidationSet = validationSet; IsValid = true; ErrorMessages = new NameValueCollection(); InvalidElements = new List <string>(); // Call the abstract Validate method for elements that are not on the // skip list foreach (string element in ElementsToValidate) { if (!skipElements.Contains(element)) { Validate(element); } } if (ErrorMessages.Count > 0) { IsValid = false; } return(IsValid); }
/// <summary> /// Initialzes a new instance of the ValidationSetAttribute class with the type /// of the assoicated validation set. /// </summary> public ValidationSetAttribute(Type validationSetType) { if (!typeof(ValidationSet).IsAssignableFrom(validationSetType)) { throw new ArgumentException("The parameter validationSetType must derive from the ValidationSet class"); } // Creates an instance of the validation set ValidationSet = (ValidationSet)Activator.CreateInstance(validationSetType); }
static bool ValidateForm(Controller controller, ValidationSet vs) { // Validate HTML form (Request.Form) and save the result in the ViewData collection if (vs.Validate(controller.Request.Form)) { controller.ViewData[vs.GetType().Name + ".ErrorMessages"] = null; } else { controller.ViewData[vs.GetType().Name + ".ErrorMessages"] = vs.ErrorMessages; } return(vs.IsValid); }
/// <summary> /// Validates the given element using the Values collection and generates an error if /// invalid. /// </summary> protected override void Validate(string element) { // TODO: Call method with parameters or null MethodInfo mi = ValidationSet.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic) .Where(m => m.Name.ToLower() == MethodName.ToLower()).SingleOrDefault(); if (mi != null && mi.ReturnType == typeof(bool) && mi.GetParameters().Count() == 0) { if (!(bool)mi.Invoke(ValidationSet, null)) { InsertError(element); } } }
/// <summary> /// Initializes the current validator object with the specified validation set and /// type count. /// </summary> public void Initialize(ValidationSet validationSet, int typeCount) { if (validationSet == null) { throw new ArgumentNullException("validationSet"); } if (typeCount < 0) { throw new ArgumentNullException("typeCount"); } ValidationSet = validationSet; TypeCount = typeCount; if (string.IsNullOrEmpty(ErrorMessageFormat)) { ErrorMessageFormat = ValidationSet.GetLocalizedText(GetType().Name + "_DefaultErrorMessage"); } if (string.IsNullOrEmpty(ErrorMessageFormat)) { ErrorMessageFormat = GetDefaultErrorMessageFormat(); } }
/// <summary> /// Gets the message for the given element used by the jQuery validation plugin. /// </summary> public override string GetClientMessage(string element) { return(string.Format("equalTo:'{0}'", GetLocalizedErrorMessage(element, ValidationSet.GetLocalizedText(ReferenceElement))).Replace("'", "\'")); }
/// <summary> /// Gets the localized label text for the given element. /// </summary> protected virtual string GetLocalizedLabel(string element) { string label = ValidationSet.GetLocalizedText(element); return(string.IsNullOrEmpty(label) ? element : label); }
/// <summary> /// Gets the default error message format in English and is called if no error message is /// defined in code or in App_GlobalResources. /// </summary> protected virtual string GetDefaultErrorMessageFormat() { return(ValidationSet.GetType().Name + "." + GetType().Name); }