public static IDictionary <string, object> GetValidationAttributes( Property property, ControllerContext context) { // create own metadata based on Property object var metadata = new ModelMetadata( ModelMetadataProviders.Current, property.Entity.Type, null, property.TypeInfo.OriginalType, property.Name); metadata.DisplayName = property.Display; var clientRules = ModelValidatorProviders.Providers .GetValidators(metadata, context) .SelectMany(v => v.GetClientValidationRules()); var provider = new PropertyModelValidatorProvider(); var modelValidators = provider .GetValidatorsForAttributes(metadata, context, property.Validators); clientRules = clientRules .Union(modelValidators.SelectMany(x => x.GetClientValidationRules())) .DistinctBy(x => x.ValidationType); var validationAttributes = new Dictionary <string, object>(); UnobtrusiveValidationAttributesGenerator .GetValidationAttributes(clientRules, validationAttributes); return(validationAttributes); }
internal static HtmlString GenerateHtmlFromClientValidationRules(IEnumerable <ModelClientValidationRule> clientRules) { if (!clientRules.Any()) { return(null); } var attributes = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, attributes); var stringBuilder = new StringBuilder(); foreach (var attribute in attributes) { string key = attribute.Key; // Values are already html encoded. string value = Convert.ToString(attribute.Value, CultureInfo.InvariantCulture); stringBuilder.Append(key) .Append("=\"") .Append(value) .Append('"') .Append(' '); } // Trim trailing whitespace if (stringBuilder.Length > 0) { stringBuilder.Length--; } return(new HtmlString(stringBuilder.ToString())); }
// Only render attributes if unobtrusive client-side validation is enabled, and then only if we've // never rendered validation for a field with this name in this form. Also, if there's no form context, // then we can't render the attributes (we'd have no <form> to attach them to). public static IDictionary <string, object> GetUnobtrusiveValidationAttributes(HtmlHelper htmlHelper, string name, ModelMetadata metadata) { Dictionary <string, object> results = new Dictionary <string, object>(); // The ordering of these 3 checks (and the early exits) is for performance reasons. if (!htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { return(results); } FormContext formContext = GetFormContextForClientValidation(htmlHelper); if (formContext == null) { return(results); } string fullName = htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (formContext.RenderedField(fullName)) { return(results); } formContext.RenderedField(fullName, true); //IEnumerable<ModelClientValidationRule> clientRules = htmlHelper.ClientValidationRuleFactory(name, metadata); //copy of the ClientValidationRuleFactory method IEnumerable <ModelClientValidationRule> clientRules = ModelValidatorProviders.Providers.GetValidators(metadata ?? ModelMetadata.FromStringExpression(name, htmlHelper.ViewData), htmlHelper.ViewContext).SelectMany(v => v.GetClientValidationRules()); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, results); return(results); }
// Only render attributes if client-side validation is enabled, and then only if we've // never rendered validation for a field with this name in this form. protected override IDictionary <string, object> GetValidationAttributes(ViewContext viewContext, ModelExplorer modelExplorer, string expression) { var formContext = viewContext.ClientValidationEnabled ? viewContext.FormContext : null; if (formContext == null) { return(null); } var fullName = GetFullHtmlFieldName(viewContext, expression); if (formContext.RenderedField(fullName)) { return(null); } formContext.RenderedField(fullName, true); var clientRules = GetClientValidationRules(viewContext, modelExplorer, expression); // use inbuilt Unobtrusive validation when the parsley attribute is not used if (viewContext.ViewData.ContainsKey(ParsleyValidationAttributesGenerator.USE_PARSLEY_KEY) && (bool)viewContext.ViewData[ParsleyValidationAttributesGenerator.USE_PARSLEY_KEY] == true) { return(ParsleyValidationAttributesGenerator.GetValidationAttributes(clientRules)); } else { return(UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules)); } }
public void GetValidationAttributesReturnsAttributesWhenMinAndStringLengthRulesAreApplied() { // Arrange var clientRules = new ModelClientValidationRule[] { new ModelClientValidationMinLengthRule("Min length message", 2), new ModelClientValidationStringLengthRule("String length rule", 2, 6) }; var attributes = new Dictionary <string, object>(); // Act UnobtrusiveValidationAttributesGenerator.GetValidationAttributes( clientRules, attributes ); // Assert Assert.Equal(6, attributes.Count); Assert.Equal("true", attributes["data-val"]); Assert.Equal("String length rule", attributes["data-val-length"]); Assert.Equal(2, attributes["data-val-length-min"]); Assert.Equal(6, attributes["data-val-length-max"]); Assert.Equal("Min length message", attributes["data-val-minlength"]); Assert.Equal(2, attributes["data-val-minlength-min"]); }
internal IDictionary <string, object> GetUnobtrusiveValidationAttributes(string field) { var clientRules = GetClientValidationRules(field); var attributes = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, attributes); return(attributes); }
internal IDictionary <string, object> GetUnobtrusiveValidationAttributes(string field) { IEnumerable <ModelClientValidationRule> clientValidationRules = this.GetClientValidationRules(field); Dictionary <string, object> results = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientValidationRules, results); return(results); }
/// <summary> /// Populates the input tag with unobtrusive validation attributes /// </summary> /// <param name="tag"></param> /// <param name="info"></param> /// <returns></returns> public static HtmlTag AddValidationAttributes(this HtmlTag tag, ModelInfo info) { if (!tag.IsInputElement()) { return(tag); } var all = ModelValidatorProviders.Providers.GetValidators(info.Meta, info.ViewContext) .SelectMany(d => d.GetClientValidationRules()); var attr = new Dictionary <string, object>(); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(all, attr); foreach (var kv in attr) { tag.Attr(kv.Key, kv.Value); } return(tag); }
public static HtmlString GenerateHtmlAttributes(this IEnumerable <ModelClientValidationRule> clientRules) { if (!clientRules.Any <ModelClientValidationRule>()) { return(null); } Dictionary <string, object> results = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, results); StringBuilder builder = new StringBuilder(); foreach (KeyValuePair <string, object> pair in results) { string key = pair.Key; string str2 = HttpUtility.HtmlEncode(Convert.ToString(pair.Value, CultureInfo.InvariantCulture)); builder.Append(key).Append("=\"").Append(str2).Append('"').Append(' '); } if (builder.Length > 0) { builder.Length--; } return(new HtmlString(builder.ToString())); }