/// <summary> /// Adds HTML attributes and name= attribute to a tag. /// </summary> /// <param name="tag">The tag to which attributes are added.</param> /// <param name="fieldType">The type of the field.</param> /// <remarks>This is used for the main tag of a template. /// /// Also adds validation attributes depending on the field's type.</remarks> public void FieldSetup(YTagBuilder tag, FieldType fieldType) { if (HtmlAttributes != null) { tag.MergeAttributes(HtmlAttributes, false); } switch (fieldType) { case FieldType.Anonymous: break; case FieldType.Normal: tag.MergeAttribute("name", FieldName, false); break; case FieldType.Validated: tag.MergeAttribute("name", FieldName, false); // error state AddErrorClass(tag); // client side validation AddValidation(tag); break; } }
/// <summary> /// Renders the beginning <form> tag with the specified attributes. /// </summary> /// <param name="HtmlAttributes">The HTML attributes to add to the <form> tag.</param> /// <param name="SaveReturnUrl">Defines whether the return URL is saved when the form is submitted.</param> /// <param name="ValidateImmediately">Defines whether client-side validation is immediate (true) or delayed until form submission (false).</param> /// <param name="ActionName">Overrides the default action name.</param> /// <param name="ControllerName">Overrides the default controller name.</param> /// <param name="Pure">TODO: Purpose unclear.</param> /// <param name="Method">The method used to submit the form (get/post)</param> /// <returns>Returns the HTML with the generated <form> tag.</returns> protected async Task <string> RenderBeginFormAsync(object HtmlAttributes = null, bool SaveReturnUrl = false, bool ValidateImmediately = false, string ActionName = null, string ControllerName = null, string Method = "post") { await YetaWFCoreRendering.Render.AddFormsAddOnsAsync(); await Manager.AddOnManager.AddAddOnNamedAsync("YetaWF_Core", "Forms");// standard css, validation strings await Manager.AddOnManager.AddAddOnNamedAsync("YetaWF_ComponentsHTML", "Forms"); Manager.ScriptManager.AddLast("$YetaWF.Forms", "$YetaWF.Forms;");// need to evaluate for side effect to initialize forms Manager.NextUniqueIdPrefix(); if (string.IsNullOrWhiteSpace(ActionName)) { ActionName = GetViewName(); } if (!ActionName.EndsWith(YetaWFViewExtender.PartialSuffix)) { ActionName += YetaWFViewExtender.PartialSuffix; } if (string.IsNullOrWhiteSpace(ControllerName)) { ControllerName = ModuleBase.Controller; } IDictionary <string, object> rvd = YHtmlHelper.AnonymousObjectToHtmlAttributes(HtmlAttributes); if (SaveReturnUrl) { rvd.Add(Basics.CssSaveReturnUrl, ""); } string css = null; if (Manager.CurrentSite.FormErrorsImmed) { css = CssManager.CombineCss(css, "yValidateImmediately"); } css = CssManager.CombineCss(css, Forms.CssFormAjax); rvd.Add("class", css); YTagBuilder tagBuilder = new YTagBuilder("form"); tagBuilder.MergeAttributes(rvd, true); if (ModuleBase.FormAutoComplete) { tagBuilder.Attributes.Add("autocomplete", "on"); } else { tagBuilder.Attributes.Add("autocomplete", "new-password"); } string id = null; if (tagBuilder.Attributes.ContainsKey("id")) { id = (string)tagBuilder.Attributes["id"]; } else { id = Manager.UniqueId(); tagBuilder.Attributes.Add("id", id); } string formAction; #if MVC6 System.IServiceProvider services = HtmlHelper.ActionContext.HttpContext.RequestServices; IUrlHelper urlHelper = services.GetRequiredService <IUrlHelperFactory>().GetUrlHelper(HtmlHelper.ActionContext); formAction = urlHelper.Action(action: ActionName, controller: ControllerName, new { area = HtmlHelper.RouteData.Values["area"] }); #else formAction = UrlHelper.GenerateUrl(null /* routeName */, ActionName, ControllerName, null, RouteTable.Routes, HtmlHelper.RequestContext, true /* includeImplicitMvcValues */); #endif tagBuilder.MergeAttribute("action", formAction, true); tagBuilder.MergeAttribute("method", Method, true); // show errors if already present if (!HtmlHelper.ModelState.IsValid) { Manager.ScriptManager.AddLast($@" var f = $YetaWF.getElementById('{id}'); if ($YetaWF.Forms.hasErrors(f)) $YetaWF.Forms.showErrors(f); "); } return(tagBuilder.ToString(YTagRenderMode.StartTag)); }