/// <summary> /// Generate Ajax form tag for ajax based form submission. Experimental. /// </summary> /// <param name="parameters"></param> /// <returns></returns> public string AjaxFormTag(IDictionary parameters) { currentFormId = CommonUtils.ObtainEntryAndRemove(parameters, "id", "form" + ++formCount); validationConfig = validatorProvider.CreateConfiguration(parameters); string afterFormTag = IsValidationEnabled ? validationConfig.CreateAfterFormOpened(currentFormId) : String.Empty; string url = UrlHelper.For(parameters); parameters["form"] = true; if (parameters.Contains("onsubmit")) { string onSubmitFunc = CommonUtils.ObtainEntryAndRemove(parameters, "onsubmit"); //remove return to make it compatible for ajax condition if (onSubmitFunc.StartsWith("return ", StringComparison.InvariantCultureIgnoreCase)) { onSubmitFunc = onSubmitFunc.Substring(7); } if (onSubmitFunc.EndsWith(";", StringComparison.InvariantCultureIgnoreCase)) { onSubmitFunc = onSubmitFunc.Remove(onSubmitFunc.Length - 1); } string conditionFunc = CommonUtils.ObtainEntryAndRemove(parameters, "condition", string.Empty); if (!string.IsNullOrEmpty(conditionFunc)) { conditionFunc += " && "; } conditionFunc += onSubmitFunc; parameters["condition"] = conditionFunc; } bool isMethodAssigned = parameters.Contains("method"); string method = CommonUtils.ObtainEntryAndRemove(parameters, "method", "post"); parameters["url"] = url; // reassign method so in case if there is no value the default is assigned. if (isMethodAssigned) { parameters["method"] = method; } String remoteFunc = new AjaxHelper().RemoteFunction(parameters); string formContent = String.Format("<form id='{1}' method='{2}' {3} onsubmit=\"{0}; return false;\" enctype=\"multipart/form-data\">", remoteFunc, currentFormId, method,GetAttributes(parameters)); return formContent + afterFormTag; }
/// <summary> /// Creates a form tag based on the parameters. /// <para> /// Javascript validation can also be bound to /// the form and|or elements nested as long as the helper is /// able to reach the <see cref="Type"/> of the object used on your view code /// </para> /// <para> /// The action attribute generation will use <see cref="UrlHelper"/> /// </para> /// </summary> /// /// <example> /// /// <code lang="none"> /// $Form.FormTag('mytarget.castle', "%{id='productform'}") /// </code> /// /// Outputs: /// /// <code lang="xml"> /// <form method='post' action='mytarget.castle' id='productform'> /// </code> /// /// </example> /// /// <remarks> /// The following parameters are accepted. /// /// <list type="table"> /// <term> /// <term>method</term> /// <description>string. The http method to use. Defaults to <c>post</c></description> /// </term> /// <term> /// <term>id</term> /// <description>string. The form id.</description> /// </term> /// </list> /// /// More parameters can be accepted depending on the form validation strategy you are using (if any). /// /// </remarks> /// /// <param name="url">The hardcoded url.</param> /// <param name="parameters">The parameters for the tag or for action and form validation generation.</param> /// <returns></returns> public string FormTag(string url, IDictionary parameters) { string method = CommonUtils.ObtainEntryAndRemove(parameters, "method", "post"); currentFormId = CommonUtils.ObtainEntryAndRemove(parameters, "id", "form" + ++formCount); validationConfig = validatorProvider.CreateConfiguration(parameters); string afterFormTag = IsValidationEnabled ? validationConfig.CreateAfterFormOpened(currentFormId) : String.Empty; string formContent; if (url != null) { formContent = "<form action='" + url + "' method='" + method + "' " + "id='" + currentFormId + "' " + GetAttributes(parameters) + ">"; } else { formContent = "<form method='" + method + "' id='" + currentFormId + "' " + GetAttributes(parameters) + ">"; } return formContent + afterFormTag; }