public void ScriptBlockGeneratesValidatableXHTML()
        {
            const string script      = "var i = 1;";
            var          scriptBlock = AbstractHelper.ScriptBlock(script);

            Assert.AreEqual("\r\n<script type=\"text/javascript\">/*<![CDATA[*/\r\n" + script + "/*]]>*/</script>\r\n", scriptBlock);
        }
            /// <summary>
            /// Implementors should return any tag/js content
            /// to be rendered after the form tag is closed.
            /// </summary>
            /// <param name="formId">The form id.</param>
            /// <returns></returns>
            public override string CreateBeforeFormClosed(string formId)
            {
                var sb = new StringBuilder();

                sb.Append("if (!window.prototypeValidators) prototypeValidators = new Object();\n");
                sb.AppendFormat("var validator = new Validation('{0}', {1});\n", formId, AjaxHelper.JavascriptOptions(jsOptions));
                sb.AppendFormat("prototypeValidators['{0}'] = validator;\n", formId);

                if (rules.Count != 0)
                {
                    sb.Append("Validation.addAllThese([\n");

                    var addedFirstRule = false;
                    var Comma          = "";

                    foreach (var rule in rules.Values)
                    {
                        sb.AppendFormat("{0} ['{1}', '{2}', {{ {3} }}]\n",
                                        Comma, rule.className,
                                        rule.violationMessage != null ? rule.violationMessage.Replace("'", "\'") : null,
                                        rule.rule);

                        if (!addedFirstRule)
                        {
                            addedFirstRule = true;
                            Comma          = ",";
                        }
                    }

                    sb.Append("]);\n");
                }

                return(AbstractHelper.ScriptBlock(sb.ToString()));
            }
Ejemplo n.º 3
0
 private string WrapProxyIfNeeded(string result)
 {
     if (WrapProxyInScriptBlock)
     {
         return(AbstractHelper.ScriptBlock(result));
     }
     return(result);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Render the validation init script
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        public override string CreateAfterFormOpened(string formId)
        {
            var display = CommonUtils.ObtainEntryAndRemove(jsOptions, "display");

            var script = new StringBuilder();

            script.Append("$('" + formId + "').setAttribute('z:options','" + AjaxHelper.JavascriptOptions(jsOptions) + "')");
            script.AppendLine(";");

            script.Append("$('" + formId + "').setAttribute('z:display','" + display + "')");
            script.AppendLine(";");

            return(AbstractHelper.ScriptBlock(script.ToString()));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Called by the framework so the component can
 /// render its content
 /// </summary>
 public override void Render()
 {
     Context.Writer.WriteLine(AbstractHelper.ScriptBlock(GenerateJS()));
 }
Ejemplo n.º 6
0
            /// <summary>
            /// Implementors should return any tag/js content
            /// to be rendered after the form tag is closed.
            /// </summary>
            /// <param name="formId">The form id.</param>
            /// <returns></returns>
            public override string CreateBeforeFormClosed(string formId)
            {
                var stringBuilder = new StringBuilder();

                if (_rules.Count > 0)
                {
                    _options[JQueryOptions.Rules] = AbstractHelper.JavascriptOptions(_rules);
                }

                if (_messages.Count > 0)
                {
                    _options[JQueryOptions.Messages] = AbstractHelper.JavascriptOptions(_messages);
                }

                bool isAjax;

                bool.TryParse(CommonUtils.ObtainEntryAndRemove(
                                  _options,
                                  JQueryOptions.IsAjax,
                                  bool.FalseString), out isAjax);

                if (isAjax)
                {
                    var submitHandler = CommonUtils.ObtainEntryAndRemove(_options, JQueryOptions.SubmitHandler);

                    if (submitHandler == null)
                    {
                        if (_ajaxOptions.Count > 0)
                        {
                            _options.Add(
                                JQueryOptions.SubmitHandler,
                                string.Concat(
                                    "function( form ) { jQuery( form ).ajaxSubmit( ",
                                    AbstractHelper.JavascriptOptions(_ajaxOptions),
                                    "); }"));
                        }
                        else
                        {
                            _options.Add(
                                JQueryOptions.SubmitHandler,
                                "function( form ) { jQuery( form ).ajaxSubmit(); }");
                        }
                    }
                }

                MergeGroupDefinitionsWithOptions();
                GenerateGroupNotEmptyValidatorCustomRule();
                GenerateGroupNotEmptyValidatorCustomClass();

                stringBuilder.Append("jQuery( document ).ready( function() { ");
                stringBuilder.AppendFormat("jQuery(\"#{0}\").validate( {1} );", formId, AbstractHelper.JavascriptOptions(_options));

                if (_customRules.Count > 0)
                {
                    foreach (var rule in _customRules.Values)
                    {
                        stringBuilder.Append(Environment.NewLine);
                        stringBuilder.Append("jQuery.validator.addMethod('");
                        stringBuilder.Append(rule.Name);
                        stringBuilder.Append("', ");
                        stringBuilder.Append(rule.Rule);
                        stringBuilder.Append(", '");
                        stringBuilder.Append(rule.ViolationMessage);
                        stringBuilder.Append("' );");
                    }
                }

                if (_customClasses.Count > 0)
                {
                    foreach (var pair in _customClasses)
                    {
                        stringBuilder.Append(Environment.NewLine);
                        stringBuilder.Append("jQuery.validator.addClassRules({");
                        stringBuilder.Append(string.Format("required{0}", pair.Key));
                        stringBuilder.Append(": ");
                        stringBuilder.Append(AbstractHelper.JavascriptOptions(pair.Value));
                        stringBuilder.Append("});");
                    }
                }

                stringBuilder.Append(" });");

                return(AbstractHelper.ScriptBlock(stringBuilder.ToString()));
            }