/// <summary> /// 创建 /// </summary> /// <param name="viewMode"></param> /// <param name="buttons"></param> public FormHorizontal(bool viewMode, IList <IHtmlElement> buttons) : base(HtmlTag.Form) { AddClass("form-horizontal"); Attribute(HtmlAttribute.Method, "post"); Script = new Input.ScriptRegister(); Validator = new FormValidate.FormValidator(); ViewMode = viewMode; Buttons = buttons ?? new List <IHtmlElement>(); }
/// <summary> /// 设置验证 /// </summary> /// <param name="form"></param> /// <param name="inputName"></param> internal void SetValidate(FormValidate.FormValidator form, string inputName) { if (ReadOnly || Disabled) { return; } var validator = form[inputName]; if (Required != null && !Required.AllowEmptyStrings) { validator.Set(Validator.NoEmpty().SetMessage(Required)); } var stringLength = Attributes.Get <StringLengthAttribute>(); var minLength = Attributes.Get <MinLengthAttribute>(); var maxLength = Attributes.Get <MaxLengthAttribute>(); if (stringLength != null) { if (stringLength.MinimumLength > 0 && stringLength.MaximumLength == int.MaxValue) { validator.Set(Validator.StringLength(stringLength.MinimumLength).SetMessage(stringLength)); } else { validator.Set(Validator.StringLength(stringLength.MinimumLength, stringLength.MaximumLength).SetMessage(stringLength)); } } else if (minLength != null && maxLength != null) { validator.Set(Validator.StringLength(minLength.Length, maxLength.Length).SetMessage(maxLength)); } else if (minLength != null) { validator.Set(Validator.StringLength(minLength.Length).SetMessage(minLength)); } else if (maxLength != null) { validator.Set(Validator.StringLength(0, maxLength.Length).SetMessage(maxLength)); } var regular = Attributes.Get <RegularExpressionAttribute>(); if (regular != null && !string.IsNullOrEmpty(regular.Pattern)) { validator.Set(Validator.Regexp(regular.Pattern).SetMessage(regular)); } if (DataType == DataType.EmailAddress) { validator.Set(Validator.EmailAddress().Message(DataTypeErrorMessage)); } var compare = Attributes.Get <CompareAttribute>(); if (compare != null && !string.IsNullOrEmpty(compare.OtherProperty)) { validator.Set(Validator.Identical(compare.OtherProperty).SetMessage(compare)); form[compare.OtherProperty].Set(Validator.Identical(inputName).SetMessage(compare)); } var range = Attributes.Get <RangeAttribute>(); if (range != null) { validator.Set(Validator.GreaterThan(range.Minimum).SetMessage(range)); validator.Set(Validator.LessThan(range.Maximum).SetMessage(range)); } var fixedCount = Attributes.Get <Annotations.FixedCountAttribute>(); if (fixedCount != null) { validator.Set(Validator.FixedCount(fixedCount.Value).Message(string.Format(fixedCount.ErrorMessage ?? "数量限定 {0} 个", fixedCount.Value))); } var rangeCount = Attributes.Get <Annotations.RangeCountAttribute>(); if (rangeCount != null) { validator.Set(Validator.RangeCount(rangeCount.MinCount, rangeCount.MaxCount, rangeCount.Inclusive).Message(rangeCount.ErrorMessage ?? "数量限定有误")); } }