Ejemplo n.º 1
0
		/// <summary>
		/// 构建表单组的html
		/// 包含
		///		名称,控件,验证文本
		/// 结构
		/// div.form-group
		///		label.control-label
		///		div.form-controls
		///			控件
		///			span.field-validation-valid
		/// </summary>
		/// <param name="field">表单字段</param>
		/// <param name="htmlAttributes">添加到元素的html属性</param>
		/// <param name="control">控件</param>
		/// <returns></returns>
		public virtual string FormGroupHtml(
			FormField field, Dictionary<string, string> htmlAttributes, string control) {
			var html = new StringBuilder();
			html.AppendLine("<div class='form-group'>");
			html.AppendLine("	<label class='control-label'>");
			html.AppendLine("		" + HttpUtility.HtmlEncode(new T(field.Attribute.Name)));
			if (htmlAttributes.ContainsKey(Required.Key)) {
				html.AppendLine("		<em>*</em>");
			}
			html.AppendLine("	</label>");
			html.AppendLine("	<div class='form-controls'>");
			html.AppendLine("		" + control);
			html.AppendLine("		<span class='field-validation-valid' data-valmsg-for='" +
									HttpUtility.HtmlAttributeEncode(field.Attribute.Name) +
									"' data-valmsg-replace='true'></span>");
			html.AppendLine("	</div>");
			html.AppendLine("</div>");
			return html.ToString();
		}
Ejemplo n.º 2
0
		/// <summary>
		/// 初始化表单构建器
		/// </summary>
		public ModelFormBuilder(FormBuilder form = null) {
			Form = form ?? Application.Ioc.Resolve<FormBuilder>();
			var type = GetType();
			// 设置表单属性
			var formAttribute = type.GetAttribute<FormAttribute>();
			if (formAttribute != null) {
				Form.Attribute = formAttribute;
			}
			// 添加成员和验证属性
			Form.Fields.Clear();
			FieldToMember = new Dictionary<FormField, MemberInfo>();
			var members = type.GetProperties().OfType<MemberInfo>().Concat(type.GetFields());
			foreach (var member in members) {
				var fieldAttribute = member.GetAttribute<FormFieldAttribute>();
				if (fieldAttribute != null) {
					var field = new FormField(fieldAttribute);
					field.ValidationAttributes.AddRange(member.GetAttributes<ValidationAttribute>());
					Form.Fields.Add(field);
					FieldToMember[field] = member;
				}
			}
		}
Ejemplo n.º 3
0
		/// <summary>
		/// 描画表单字段
		/// </summary>
		/// <param name="html">html构建器</param>
		/// <param name="field">表单字段</param>
		protected virtual void RenderFormField(HtmlTextWriter html, FormField field) {
			// 根据验证器添加html属性
			var htmlAttributes = new Dictionary<string, string>();
			foreach (var validatorAttribute in field.ValidationAttributes) {
				var validator = Application.Ioc.Resolve<IFormFieldValidator>(serviceKey: validatorAttribute.GetType());
				validator.AddHtmlAttributes(field, validatorAttribute, htmlAttributes);
			}
			// 构建表单字段的html并写入到html构建器
			var fieldHandler = Application.Ioc.Resolve<IFormFieldHandler>(serviceKey: field.Attribute.GetType());
			html.WriteLine(fieldHandler.Build(field, htmlAttributes));
		}
Ejemplo n.º 4
0
		/// <summary>
		/// 描画csrf校验值
		/// </summary>
		/// <param name="html">html构建器</param>
		protected virtual void RenderCsrfToken(HtmlTextWriter html) {
			// 不需要描画时直接返回
			if (!Attribute.EnableCsrfToken) {
				return;
			}
			// 获取cookies传入的csrf校验值,不存在时设置
			var token = HttpContextUtils.GetCookie(CsrfTokenKey);
			if (string.IsNullOrEmpty(token)) {
				token = HttpServerUtility.UrlTokenEncode(RandomUtils.SystemRandomBytes(20));
				HttpContextUtils.PutCookie(CsrfTokenKey, token);
			}
			// 添加到表单中
			var field = new FormField(new HiddenFieldAttribute(CsrfTokenFieldName));
			field.Value = token;
			RenderFormField(html, field);
		}