public static void AddModelError(this ModelStateDictionary modelState, int errorCode, string propertyName = null, object parameters = null) { //Try and get the custom error CustomErrorMessage customError = CustomErrorMessages.GetError(errorCode); if (customError == null) { throw new ArgumentException("errorCode", "Cannot find custom error message with this code"); } //Add the error to the modelstate string title = customError.Title; string description = customError.Description; //Resolve the parameters if (parameters != null) { title = parameters.Resolve(title); description = parameters.Resolve(description); } //add the summary message if it doesnt already exist if (!string.IsNullOrWhiteSpace(title) && !modelState.Any(m => m.Key == "" && m.Value.Errors.Any(e => e.ErrorMessage == title))) { modelState.AddModelError("", title); } if (!string.IsNullOrWhiteSpace(description)) { //If no property then add description as second line of summary if (string.IsNullOrWhiteSpace(propertyName)) { if (!string.IsNullOrWhiteSpace(title) && !modelState.Any(m => m.Key == "" && m.Value.Errors.Any(e => e.ErrorMessage == title))) { modelState.AddModelError("", title); } } //add the inline message if it doesnt already exist else if (!modelState.Any(m => m.Key.EqualsI(propertyName) && m.Value.Errors.Any(e => e.ErrorMessage == description))) { modelState.AddModelError(propertyName, description); } } }
private static Dictionary <string, object> CustomAttributesFor <TModel, TProperty>(IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes = null) { Type containerType = typeof(TModel); string propertyName = GetModelExpressionProvider(htmlHelper).GetExpressionText(expression); PropertyInfo propertyInfo = containerType.GetPropertyInfo(propertyName); var displayNameAttribute = propertyInfo?.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute; var displayAttribute = propertyInfo?.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; string displayName = displayNameAttribute != null ? displayNameAttribute.DisplayName : displayAttribute != null ? displayAttribute.Name : propertyName; string par1 = null; string par2 = null; Dictionary <string, object> htmlAttr = htmlAttributes.ToPropertyDictionary(); if (propertyInfo != null) { foreach (ValidationAttribute attribute in propertyInfo.GetCustomAttributes(typeof(ValidationAttribute), false)) { string validatorKey = $"{containerType.Name}.{propertyName}:{attribute.GetType().Name.TrimSuffix("Attribute")}"; CustomErrorMessage customError = CustomErrorMessages.GetValidationError(validatorKey); if (customError == null) { continue; } //Set the message from the description if (attribute.ErrorMessage != customError.Description) { attribute.ErrorMessage = customError.Description; } //Set the inline error message var errorMessageString = Misc.GetPropertyValue(attribute, "ErrorMessageString") as string; if (string.IsNullOrWhiteSpace(errorMessageString)) { errorMessageString = attribute.ErrorMessage; } //Set the summary error message if (customError.Title != errorMessageString) { errorMessageString = customError.Title; } //Set the display name if (!string.IsNullOrWhiteSpace(customError.DisplayName) && customError.DisplayName != displayName) { if (displayAttribute != null) { Misc.SetPropertyValue(displayAttribute, "Name", customError.DisplayName); } displayName = customError.DisplayName; } string altAttr = null; if (attribute is RequiredAttribute) { altAttr = "data-val-required-alt"; } else if (attribute is CompareAttribute) { altAttr = "data-val-equalto-alt"; } else if (attribute is RegularExpressionAttribute) { altAttr = "data-val-regex-alt"; } else if (attribute is RangeAttribute) { altAttr = "data-val-range-alt"; par1 = ((RangeAttribute)attribute).Minimum.ToString(); par2 = ((RangeAttribute)attribute).Maximum.ToString(); } else if (attribute is DataTypeAttribute) { string type = ((DataTypeAttribute)attribute).DataType.ToString().ToLower(); switch (type) { case "password": continue; case "emailaddress": type = "email"; break; case "phonenumber": type = "phone"; break; } altAttr = $"data-val-{type}-alt"; } else if (attribute is MinLengthAttribute) { altAttr = "data-val-minlength-alt"; par1 = ((MinLengthAttribute)attribute).Length.ToString(); } else if (attribute is MaxLengthAttribute) { altAttr = "data-val-maxlength-alt"; par1 = ((MaxLengthAttribute)attribute).Length.ToString(); } else if (attribute is StringLengthAttribute) { altAttr = "data-val-length-alt"; par1 = ((StringLengthAttribute)attribute).MinimumLength.ToString(); par2 = ((StringLengthAttribute)attribute).MaximumLength.ToString(); } htmlAttr[altAttr.TrimSuffix("-alt")] = string.Format(attribute.ErrorMessage, displayName, par1, par2); htmlAttr[altAttr] = string.Format(errorMessageString, displayName, par1, par2); } } return(htmlAttr); }
public void Insert(int index, CustomErrorMessage element) { base.BaseAdd(index, element); }
public static void CleanModelErrors <TModel>(this Controller controller) { Type containerType = typeof(TModel); //Save the old modelstate var oldModelState = new ModelStateDictionary(); foreach (KeyValuePair <string, ModelStateEntry> modelState in controller.ModelState) { string propertyName = modelState.Key; foreach (ModelError error in modelState.Value.Errors) { bool exists = oldModelState.Any( m => m.Key == propertyName && m.Value.Errors.Any(e => e.ErrorMessage == error.ErrorMessage)); //add the inline message if it doesnt already exist if (!exists) { oldModelState.AddModelError(propertyName, error.ErrorMessage); } } } //Clear the model state ready for refill controller.ModelState.Clear(); foreach (KeyValuePair <string, ModelStateEntry> modelState in oldModelState) { //Get the property name string propertyName = modelState.Key; //Get the validation attributes PropertyInfo propertyInfo = string.IsNullOrWhiteSpace(propertyName) ? null : containerType.GetPropertyInfo(propertyName); List <ValidationAttribute> attributes = propertyInfo == null ? null : propertyInfo.GetCustomAttributes(typeof(ValidationAttribute), false).ToList <ValidationAttribute>(); //Get the display name var displayNameAttribute = propertyInfo?.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute; var displayAttribute = propertyInfo?.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; string displayName = displayNameAttribute != null ? displayNameAttribute.DisplayName : displayAttribute != null ? displayAttribute.Name : propertyName; foreach (ModelError error in modelState.Value.Errors) { string title = string.IsNullOrWhiteSpace(propertyName) ? error.ErrorMessage : null; string description = !string.IsNullOrWhiteSpace(propertyName) ? error.ErrorMessage : null; if (error.ErrorMessage.Like("The value * is not valid for *.")) { title = "There's a problem with your values."; description = "The value here is invalid."; } if (attributes != null && attributes.Any()) { ValidationAttribute attribute = attributes.FirstOrDefault(a => a.FormatErrorMessage(displayName) == error.ErrorMessage); if (attribute != null) { string validatorKey = $"{containerType.Name}.{propertyName}:{attribute.GetType().Name.TrimSuffix("Attribute")}"; CustomErrorMessage customError = CustomErrorMessages.GetValidationError(validatorKey); if (customError != null) { title = attribute.FormatError(customError.Title, displayName); description = attribute.FormatError(customError.Description, displayName); } } } //add the summary message if it doesnt already exist if (!string.IsNullOrWhiteSpace(title) && !controller.ModelState.Any(m => m.Key == "" && m.Value.Errors.Any(e => e.ErrorMessage == title))) { controller.ModelState.AddModelError("", title); } //add the inline message if it doesnt already exist if (!string.IsNullOrWhiteSpace(description) && !string.IsNullOrWhiteSpace(propertyName) && !controller.ModelState.Any( m => m.Key.EqualsI(propertyName) && m.Value.Errors.Any(e => e.ErrorMessage == description))) { controller.ModelState.AddModelError(propertyName, description); } } } }
public void Add(CustomErrorMessage element) { base.BaseAdd(element); }