コード例 #1
0
        // 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 IDictionary <string, object> GetUnobtrusiveValidationAttributes(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 (!ViewContext.UnobtrusiveJavaScriptEnabled)
            {
                return(results);
            }

            FormContext formContext = ViewContext.GetFormContextForClientValidation();

            if (formContext == null)
            {
                return(results);
            }

            string fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (formContext.RenderedField(fullName))
            {
                return(results);
            }

            formContext.RenderedField(fullName, true);

            IEnumerable <ModelClientValidationRule> clientRules = ClientValidationRuleFactory(name, metadata);

            UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, results);

            return(results);
        }
コード例 #2
0
ファイル: HtmlHelper.cs プロジェクト: pmq20/mono_forked
        // 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 IDictionary <string, object> GetUnobtrusiveValidationAttributes(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 (!ViewContext.UnobtrusiveJavaScriptEnabled)
            {
                return(results);
            }

            FormContext formContext = ViewContext.GetFormContextForClientValidation();

            if (formContext == null)
            {
                return(results);
            }

            string fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (formContext.RenderedField(fullName))
            {
                return(results);
            }

            formContext.RenderedField(fullName, true);

            IEnumerable <ModelClientValidationRule> clientRules = ClientValidationRuleFactory(name, metadata);
            bool renderedRules = false;

            foreach (ModelClientValidationRule rule in clientRules)
            {
                renderedRules = true;
                string ruleName = "data-val-" + rule.ValidationType;

                ValidateUnobtrusiveValidationRule(rule, results, ruleName);

                results.Add(ruleName, HttpUtility.HtmlEncode(rule.ErrorMessage ?? String.Empty));
                ruleName += "-";

                foreach (var kvp in rule.ValidationParameters)
                {
                    results.Add(ruleName + kvp.Key, kvp.Value ?? String.Empty);
                }
            }

            if (renderedRules)
            {
                results.Add("data-val", "true");
            }

            return(results);
        }