コード例 #1
0
        /// <summary>
        /// Registers the scripts.
        /// </summary>
        /// <param name="rules">The rules.</param>
        public void RegisterScripts(RuleCollection rules)
        {
            ClientScriptManager scriptManager = _page.ClientScript;

            string initScriptResourceUrl = scriptManager.GetWebResourceUrl(_registerType, ModelPropertyValidator.WebformValidateResourceName);

            scriptManager.RegisterClientScriptInclude(_registerType, "WebformValidate", initScriptResourceUrl);

            string validateInitScript = String.Format("$(document).ready(function(){{ $webformValidate.init('#{0}'); }});{1}", _page.Form.ClientID, Environment.NewLine);

            scriptManager.RegisterStartupScript(_registerType, "WebformValidateInit", validateInitScript, true);

            StringBuilder validationOptionsScript = new StringBuilder();

            validationOptionsScript.AppendFormat("$(document).ready(function(){{ $('#{0}').rules('add', ", _controlToValidateId);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            serializer.RegisterConverters(new[] { new RulesJavaScriptConverter() });
            serializer.Serialize(rules, validationOptionsScript);

            validationOptionsScript.AppendLine("); });");

            scriptManager.RegisterStartupScript(
                _registerType, _controlToValidateId, validationOptionsScript.ToString(), true);
        }
コード例 #2
0
        /// <summary>
        /// Registers the scripts.
        /// </summary>
        public void RegisterScripts()
        {
            // get the attriubtes
            IEnumerable <ValidationAttribute> attributes = GetValidationAttributes();

            if (!attributes.Any())
            {
                return;
            }

            // get the rules
            if (_ruleProvider == null)
            {
                _ruleProvider = new ValidationRuleProvider();
            }

            RuleCollection rules = _ruleProvider.GetRules(attributes);

            if (!rules.Any())
            {
                return;
            }

            // register validation scripts
            if (_scriptManager == null)
            {
                _scriptManager = new ValidationScriptManager(Page, GetControlRenderId(ControlToValidate));
            }

            _scriptManager.RegisterScripts(rules);
        }
コード例 #3
0
        /// <summary>
        /// Builds a dictionary of name/value pairs.
        /// </summary>
        /// <param name="rules">The rules to serialize.</param>
        /// <param name="serializer">The object that is responsible for the serialization.</param>
        /// <returns>
        /// An object that contains key/value pairs that represent the object’s data.
        /// </returns>
        public IDictionary <string, object> Serialize(RuleCollection rules, JavaScriptSerializer serializer)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }

            Dictionary <string, object> options =
                rules.ToDictionary <Rule, string, object>(rule => rule.Name, rule => rule.Options);
            Dictionary <string, string> messages =
                rules.ToDictionary(rule => rule.Name, rule => rule.Message);

            Dictionary <string, object> result =
                new Dictionary <string, object>(options)
            {
                { "messages", messages }
            };

            return(result);
        }