protected internal virtual void RenderInvalidMessageElement(IFormField formField, HtmlDocumentBuilder documentBuilder) { var divElement = new HtmlElementWriter(DivElement); var formFieldId = formField.Id; var ifExpression = string.Format(InvalidExpression + " && " + DirtyExpression, formFieldId, formFieldId); divElement.Attribute(NgIfAttribute, ifExpression).Attribute(ClassAttribute, HasErrorClass); // <div ng-if="....$invalid && ....$dirty"...> documentBuilder.StartElement(divElement); if (!IsDate(formField)) { RenderInvalidValueMessage(formField, documentBuilder); RenderInvalidTypeMessage(formField, documentBuilder); } else { RenderInvalidDateMessage(formField, documentBuilder); } documentBuilder.EndElement(); }
protected internal virtual void RenderInvalidDateMessage(IFormField formField, HtmlDocumentBuilder documentBuilder) { var formFieldId = formField.Id; var firstDivElement = new HtmlElementWriter(DivElement); var firstExpression = string.Format(RequiredErrorExpression + " && !" + DateErrorExpression, formFieldId, formFieldId); firstDivElement.Attribute(NgShowAttribute, firstExpression) .Attribute(ClassAttribute, HelpBlockClass) .TextContent(RequiredFieldMessage); documentBuilder.StartElement(firstDivElement).EndElement(); var secondDivElement = new HtmlElementWriter(DivElement); var secondExpression = string.Format(DateErrorExpression, formFieldId); secondDivElement.Attribute(NgShowAttribute, secondExpression) .Attribute(ClassAttribute, HelpBlockClass) .TextContent(InvalidDateFieldMessage); documentBuilder.StartElement(secondDivElement).EndElement(); }
protected internal virtual void AddCommonFormFieldAttributes(IFormField formField, HtmlElementWriter formControl) { var typeName = formField.TypeName; if (IsEnum(formField) || IsDate(formField)) { typeName = StringFormType.TypeName; } typeName = typeName.Substring(0, 1).ToUpper() + typeName.Substring(1); var formFieldId = formField.Id; formControl.Attribute(ClassAttribute, FormControlClass) .Attribute(NameAttribute, formFieldId) .Attribute(CamVariableTypeAttribute, typeName) .Attribute(CamVariableNameAttribute, formFieldId); // add validation constraints foreach (var constraint in formField.ValidationConstraints) { var constraintName = constraint.Name; var configuration = (string)constraint.Configuration; formControl.Attribute(constraintName, configuration); } }
/* (non-Javadoc) * @see com.itextpdf.html2pdf.attach.ITagWorker#getElementResult() */ public override IPropertyContainer GetElementResult() { if (formField == null) { if (hasChildren) { ButtonContainer button = new ButtonContainer(name); Div div = (Div)base.GetElementResult(); foreach (IElement element in div.GetChildren()) { if (element is IBlockElement) { button.Add((IBlockElement)element); } else { if (element is Image) { button.Add((Image)element); } } } div.GetChildren().Clear(); formField = button; } else { Button inputButton = new Button(name); inputButton.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, fallbackContent.ToString().Trim()); formField = inputButton; } } formField.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, flatten); return(formField); }
/// <summary> /// Adds a field to the form and attaches appropriate events to it. /// </summary> /// <param name="field">A field object.</param> /// <returns>The index of the object in the collection.</returns> public int AddField(IFormField field) { if (field == null) throw new ArgumentException("Null Argument passed."); field.PropertyChanged += field_PropertyChanged; Fields.Add(field); return Fields.IndexOf(field); }
protected internal virtual HtmlElementWriter CreateInputField(IFormField formField) { var inputField = new HtmlElementWriter(InputElement, true); AddCommonFormFieldAttributes(formField, inputField); inputField.Attribute(TypeAttribute, TextInputType); return(inputField); }
protected internal virtual ITypedValue CreateVariable(IFormField formField, IVariableScope variableScope) { var value = formField.Value; if (value != null) { return(value); } return(null); }
protected internal virtual void RenderSelectOptions(IFormField formField, HtmlDocumentBuilder documentBuilder) { //EnumFormType enumFormType = (EnumFormType)formField.Type; //IDictionary<string, string> values = enumFormType.Values; //foreach (var value in values) //{ // // <option> // var option = // (new HtmlElementWriter(OPTION_ELEMENT, false)).attribute(VALUE_ATTRIBUTE, value.Key) // .textContent(value.Value); // documentBuilder.startElement(option).endElement(); //} }
protected internal virtual void RenderInvalidValueMessage(IFormField formField, HtmlDocumentBuilder documentBuilder) { var divElement = new HtmlElementWriter(DivElement); var formFieldId = formField.Id; var expression = string.Format(RequiredErrorExpression, formFieldId); divElement.Attribute(NgShowAttribute, expression) .Attribute(ClassAttribute, HelpBlockClass) .TextContent(RequiredFieldMessage); documentBuilder.StartElement(divElement).EndElement(); }
protected internal virtual void RenderSelectOptions(IFormField formField, HtmlDocumentBuilder documentBuilder) { //EnumFormType enumFormType = (EnumFormType) formField.Type; IDictionary <string, string> values = new Dictionary <string, string>(); //= enumFormType.Values; foreach (var value in values) { // <option> var option = new HtmlElementWriter(OptionElement, false).Attribute(ValueAttribute, value.Key) .TextContent(value.Value); documentBuilder.StartElement(option).EndElement(); } }
protected internal virtual void RenderSelectBox(IFormField formField, HtmlDocumentBuilder documentBuilder) { var selectBox = new HtmlElementWriter(SelectElement, false); AddCommonFormFieldAttributes(formField, selectBox); // <select ...> documentBuilder.StartElement(selectBox); // <option ...> RenderSelectOptions(formField, documentBuilder); // </select> documentBuilder.EndElement(); }
protected internal virtual bool IsReadOnly(IFormField formField) { var validationConstraints = formField.ValidationConstraints; if (validationConstraints != null) { foreach (var validationConstraint in validationConstraints) { if ("readonly".Equals(validationConstraint.Name)) { return(true); } } } return(false); }
/* (non-Javadoc) * @see com.itextpdf.html2pdf.attach.ITagWorker#getElementResult() */ public override IPropertyContainer GetElementResult() { if (formField == null) { if (hasChildren) { Button button = new Button(name); button.SetProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE, lang); Div div = (Div)base.GetElementResult(); foreach (IElement element in div.GetChildren()) { if (element is IAccessibleElement) { AccessiblePropHelper.TrySetLangAttribute((IAccessibleElement)element, lang); } if (element is IBlockElement) { button.Add((IBlockElement)element); } else { if (element is Image) { button.Add((Image)element); } } } div.GetChildren().Clear(); formField = button; } else { InputButton inputButton = new InputButton(name); inputButton.SetProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE, lang); inputButton.SetProperty(Html2PdfProperty.FORM_FIELD_VALUE, fallbackContent.ToString().Trim()); formField = inputButton; } } formField.SetProperty(Html2PdfProperty.FORM_FIELD_FLATTEN, flatten); return(formField); }
protected internal virtual void RenderInputField(IFormField formField, HtmlDocumentBuilder documentBuilder) { var inputField = new HtmlElementWriter(InputElement, true); AddCommonFormFieldAttributes(formField, inputField); var inputType = !IsBoolean(formField) ? TextInputType : CheckboxInputType; inputField.Attribute(TypeAttribute, inputType); // add default value var defaultValue = formField.DefaultValue; if (defaultValue != null) { inputField.Attribute(ValueAttribute, defaultValue.ToString()); } // <input ... /> documentBuilder.StartElement(inputField).EndElement(); }
protected internal virtual void RenderFormField(IFormField formField, HtmlDocumentBuilder documentBuilder) { // start group var divElement = new HtmlElementWriter(DivElement).Attribute(ClassAttribute, FormGroupClass); documentBuilder.StartElement(divElement); var formFieldId = formField.Id; var formFieldLabel = formField.Label; // write label if (!ReferenceEquals(formFieldLabel, null) && (formFieldLabel.Length > 0)) { var labelElement = new HtmlElementWriter(LabelElement).Attribute(ForAttribute, formFieldId) .TextContent(formFieldLabel); // <label for="...">...</label> documentBuilder.StartElement(labelElement).EndElement(); } // render form control if (IsEnum(formField)) { RenderSelectBox(formField, documentBuilder); } else if (IsDate(formField)) { RenderDatePicker(formField, documentBuilder); } else { RenderInputField(formField, documentBuilder); } RenderInvalidMessageElement(formField, documentBuilder); // end group documentBuilder.EndElement(); }
protected internal virtual void RenderInvalidTypeMessage(IFormField formField, HtmlDocumentBuilder documentBuilder) { var divElement = new HtmlElementWriter(DivElement); var formFieldId = formField.Id; var expression = string.Format(TypeErrorExpression, formFieldId); var typeName = formField.TypeName; if (IsEnum(formField)) { typeName = StringFormType.TypeName; } divElement.Attribute(NgShowAttribute, expression) .Attribute(ClassAttribute, HelpBlockClass) .TextContent(string.Format(TypeFieldMessage, typeName)); documentBuilder.StartElement(divElement).EndElement(); }
/// <summary> /// Creates a new /// <see cref="AbstractTextFieldRenderer"/> /// instance. /// </summary> /// <param name="modelElement">the model element</param> internal AbstractTextFieldRenderer(IFormField modelElement) : base(modelElement) { }
public static HtmlString EditorFor(this HtmlHelper Html, IFormField field, IForm form) { object value = form?.GetValue(field.ID); var htmlAttribs = new Dictionary <string, object>() { { "class", "form-control" + (field.DataType.IsFile() ? "-file" : "") + (field.IsOptions ? " Option" : "") + (field.IsDate ? " datepicker" : "") } }; if (field.IsMandatory) { htmlAttribs.Add("required", ""); } if (field.DataType == DataTypes.MULTI_SELECT_OPTIONS) { htmlAttribs.Add("multiple", ""); } if (!string.IsNullOrWhiteSpace(field.Hint)) { htmlAttribs.Add("placeholder", field.Hint); } var @readonly = form != null && form.ID > 0 && !field.IsEditable; if (@readonly) { htmlAttribs.Add(field.IsOptions || field.IsBoolean ? "disabled" : "readonly", ""); } var validationMsg = Html.ValidationMessage(field.FieldName, new { @class = "text-danger" }).ToHtmlString(); if (field.DataType.IsString()) { if (field.MinValue.HasValue) { htmlAttribs.Add("minlength", field.MinValue.Value); } if (field.MaxValue.HasValue) { htmlAttribs.Add("maxlength", field.MaxValue.Value); } } if (field.IsDate) { if (field.MinValue.HasValue) { htmlAttribs.Add("min", new DateTime(field.MinValue.Value).ToString("yyy-MM-dd")); } if (field.MaxValue.HasValue) { htmlAttribs.Add("max", new DateTime(field.MaxValue.Value).ToString("yyy-MM-dd")); } } if (field.DataType == DataTypes.OPTIONS) { return(new HtmlString(Html.DropDownList(field.FieldName, new SelectList(field.Options, value), "Select..", htmlAttribs).ToHtmlString() + validationMsg)); } if (field.DataType == DataTypes.MULTI_SELECT_OPTIONS) { string[] values = null; try { values = JsonConvert.DeserializeObject <string[]>(value?.ToString()); } catch { } return(new HtmlString(Html.DropDownList(field.FieldName, new MultiSelectList(field.Options, values), "Select..", htmlAttribs).ToHtmlString() + validationMsg)); } else if (field.DataType == DataTypes.MULTI_LINE_TEXT || field.DataType == DataTypes.STREET_ADDRESS) { return(new HtmlString(Html.TextArea(field.FieldName, (string)value, htmlAttribs).ToHtmlString() + validationMsg)); } else if (field.DataType == DataTypes.BOOLEAN) { return(new HtmlString($"<input name='{field.FieldName}' type='checkbox' value='true' {((bool?)value == true ? "checked" : "")} {(@readonly ? "readonly" : "")} />")); } if (field.DataType.IsNumeric()) { if (field.MinValue.HasValue) { htmlAttribs.Add("min", field.MinValue.Value); } if (field.MaxValue.HasValue) { htmlAttribs.Add("max", field.MaxValue.Value); } } htmlAttribs.Add("type", field.DataType.GetInputType()); if (field.DataType == DataTypes.THUMBNAIL) { htmlAttribs.Add("accept", "image/*"); htmlAttribs.Add("capture", "user"); validationMsg += "<div class='text-info small'><i class='mdi mdi-alert'></i> Recommended size is 256x256. Larger images will be cropped!</div>"; value = null; } return(new HtmlString(Html.TextBox(field.FieldName, value, htmlAttribs).ToHtmlString() + validationMsg)); }
protected internal virtual bool IsBoolean(IFormField formField) { return(true); //return BooleanFormType.TYPE_NAME.Equals(formField.TypeName); }
protected internal virtual bool IsDate(IFormField formField) { return(DateFormType.TypeName.Equals(formField.TypeName)); }
/// <summary> /// Removes the form field object specified by duplicate within the Fields collection. /// </summary> /// <param name="field"></param> /// <returns>Whether or not the field was successfully removed from the collection.</returns> public bool RemoveField(IFormField field) { bool removeSuccessful = false; if (field == null) throw new ArgumentException("Null object argument passed."); try { field.PropertyChanged -= field_PropertyChanged; Fields.Remove(field); removeSuccessful = true; } catch { removeSuccessful = false; } return removeSuccessful; }
// helper ///////////////////////////////////////////////////////////////////////////////////// protected internal virtual bool IsEnum(IFormField formField) { return(false); //return EnumFormType.TYPE_NAME.Equals(formField.TypeName); }
protected internal virtual void RenderDatePicker(IFormField formField, HtmlDocumentBuilder documentBuilder) { //bool isReadOnly = isReadOnly(formField); // start input-group var inputGroupDivElement = new HtmlElementWriter(DivElement).Attribute(ClassAttribute, InputGroupClass); var formFieldId = formField.Id; // <div> documentBuilder.StartElement(inputGroupDivElement); // input field var inputField = CreateInputField(formField); //if (!isReadOnly) { inputField.Attribute(DatepickerPopupAttribute, DateFormat) .Attribute(IsOpenAttribute, string.Format(DateFieldOpenedAttribute, formFieldId)); } // <input ... /> documentBuilder.StartElement(inputField).EndElement(); // if form field is read only, do not render date picker open button //if (!isReadOnly) { // input addon var addonElement = new HtmlElementWriter(DivElement).Attribute(ClassAttribute, InputGroupBtnClass); // <div> documentBuilder.StartElement(addonElement); // button to open date picker var buttonElement = new HtmlElementWriter(ButtonElement).Attribute(TypeAttribute, ButtonButtonType) .Attribute(ClassAttribute, ButtonDefaultClass) .Attribute(NgClickAttribute, string.Format(OpenDatepickerFunctionSnippet, formFieldId)); // <button> documentBuilder.StartElement(buttonElement); var iconElement = new HtmlElementWriter(Element).Attribute(ClassAttribute, CalendarGlyphicon); // <i ...></i> documentBuilder.StartElement(iconElement).EndElement(); // </button> documentBuilder.EndElement(); // </div> documentBuilder.EndElement(); var scriptElement = new HtmlElementWriter(ScriptElement).Attribute(CamScriptAttribute, null) .Attribute(TypeAttribute, TextFormScriptType) .TextContent(string.Format(OpenDatepickerSnippet, formFieldId, formFieldId)); // <script ...> </script> documentBuilder.StartElement(scriptElement).EndElement(); } // </div> documentBuilder.EndElement(); }
public static HtmlString ValueDisplay(this HtmlHelper htmlHelper, IForm model, IFormField field, object htmlAttributes = null, object fileDownloadRouteValues = null, bool ignoreInexistantFiles = false) { if (field.DataType.IsFile()) { var Url = new UrlHelper(htmlHelper.ViewContext.RequestContext); var path = HttpContext.Current.Server.MapPath(model.GetFilePath(field.ID)); if (!ignoreInexistantFiles && !System.IO.File.Exists(path)) { return(new HtmlString("")); } if (field.DataType == DataTypes.THUMBNAIL) { var img = new TagBuilder("img"); if (htmlAttributes == null) { img.Attributes.Add("style", "max-width:256px"); img.AddCssClass("img-thumbnail shadow-sm"); } else { img.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } if (!img.Attributes.ContainsKey("src")) { img.Attributes.Add("src", Url.Action("GetImage", htmlHelper.ViewContext.RouteData.Values["controller"].ToString(), fileDownloadRouteValues)); } return(new HtmlString(img.ToString(TagRenderMode.SelfClosing))); } new HtmlString(""); } var value = model.GetValue(field.ID); if (field.IsBoolean) { var boolValue = (bool?)(value) == true; return(new HtmlString($"<span class='badge-pill badge badge-{(boolValue ? "success" : "danger")}'><i class='mdi mdi-{(boolValue ? "check" : "close")}'></i></span>")); } if (string.IsNullOrWhiteSpace(value?.ToString())) { return(new HtmlString("")); } if (field.DataType == DataTypes.MULTI_SELECT_OPTIONS) { try { var values = JsonConvert.DeserializeObject <string[]>(value.ToString()); var html = ""; foreach (var item in values) { html += $"<span class='badge badge-pill badge-secondary mr-1'>{item}</span>"; } return(new HtmlString(html)); } catch { } } if (field.DataType.IsURL()) { value = $"<a href='{value}' target='_blank'>{value}</a>"; } if (htmlAttributes == null) { return(new HtmlString(value.ToString())); } var span = new TagBuilder("span"); span.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); span.InnerHtml = value.ToString(); return(new HtmlString(span.ToString())); }