/// <summary> /// 创建 FormValidationResult 对象 /// </summary> /// <param name="form">所验证的表单</param> /// <param name="errors">验证错误信息</param> public FormValidationResult( HtmlForm form, IEnumerable<FormValidationError> errors ) { if ( form == null ) throw new ArgumentNullException( "form" ); Form = form; Errors = new FormValidationErrorCollection(); if ( errors != null ) { errors = errors.NotNull(); if ( errors.Any() ) { HasError = true; foreach ( var e in errors ) Errors.Add( e ); } } else HasError = false; }
/// <summary> /// 显示字段验证错误信息 /// </summary> /// <param name="form">要呈现错误信息的表单</param> /// <param name="error">要呈现的错误信息</param> protected virtual void ShowError( HtmlForm form, FormValidationError error ) { var container = FindErrorMessageContainer( form, error.Name ); if ( container != null ) ShowErrorMessage( container, error.Messages ); }
internal FormButtonGroup( HtmlForm form, string name, IHtmlElement[] buttonElements ) : base( form ) { _name = name; ButtonItems = buttonElements.Select( element => CreateGroupItem( element ) ).ToArray(); _allowMultiple = ButtonItems.Any( item => item.ButtonType == FormGroupButtonType.CheckBox ); }
public HtmlTextArea( HtmlForm form, IHtmlElement element ) { if ( !element.Name.EqualsIgnoreCase( "textarea" ) ) throw new InvalidOperationException( "只有 <textarea> 元素才能转换为 HtmlTextArea 对象" ); _form = form; _element = element; }
internal HtmlSelect( HtmlForm form, IHtmlElement element ) : base( form ) { Element = element; options = element.Find( "option" ).Select( e => new HtmlOption( this, e ) ).ToArray(); }
private HtmlButtonGroup( HtmlForm form, IGrouping<string, IHtmlElement> inputGroup ) { _form = form; name = inputGroup.Key; items = inputGroup.Select( e => new HtmlInputItem( this, e ) ).ToArray(); }
/// <summary> /// 派生类调用此构造函数初始化 FormTextControl 实例 /// </summary> /// <param name="form">所属表单</param> /// <param name="element">定义文本控件的元素</param> protected FormTextControl( HtmlForm form, IHtmlElement element ) : base( form ) { Element = element; MaxLength = GetMaxLength(); }
public static IEnumerable<HtmlButtonGroup> CaptureInputGroups( HtmlForm form ) { var inputItems = form.Element.Find( "input[type=radio][name] , input[type=checkbox][name]" ); var groups = inputItems.GroupBy( item => item.Attribute( "name" ).Value() ) .Select( item => new HtmlButtonGroup( form, item ) ); return groups; }
public HtmlLabel( HtmlForm form, IHtmlElement element ) { if ( !element.Name.EqualsIgnoreCase( "label" ) ) throw new NotSupportedException( "不能将非 label 元素转换为 HtmlLabel 对象" ); _form = form; _element = element; _forId = Element.Attribute( "for" ).Value(); }
public HtmlSelect( HtmlForm form, IHtmlElement element ) { if ( !element.Name.EqualsIgnoreCase( "select" ) ) throw new InvalidOperationException(); _form = form; _element = element; options = element.Find( "option" ).Select( e => new HtmlOption( this, e ) ).ToArray(); }
internal HtmlInputText( HtmlForm form, IHtmlElement element ) { if ( !element.Name.EqualsIgnoreCase( "input" ) ) throw new InvalidOperationException( "只有input元素才能转换为HtmlTextInput对象" ); _type = element.Attribute( "type" ).Value(); if ( !allowTypes.Contains( _type, StringComparer.OrdinalIgnoreCase ) ) throw new InvalidOperationException( "只有type为text、password或hidden的input元素才能转换为HtmlTextInput对象" ); _form = form; _element = element; }
/// <summary> /// 创建 MvcFormValidationResult 对象 /// </summary> /// <param name="form">所验证的表单</param> /// <param name="modelStates">要包装的模型验证状态</param> public MvcFormValidationResult( HtmlForm form, ModelStateDictionary modelStates ) { if ( modelStates == null ) throw new ArgumentNullException( "modelState" ); Errors = new FormValidationErrorCollection(); Form = form; modelStates.Select( CreateValidationError ).NotNull().ForAll( e => Errors.Add( e ) ); if ( Errors.Any() ) HasError = true; }
/// <summary> /// 在表单中发现所有标准控件 /// </summary> /// <param name="form">要从中发现空间的表单</param> /// <returns>发现的所有标准控件</returns> public IFormControl[] DiscoveryControls( HtmlForm form ) { var controls = DiscoveryControls( form, form.Element ); var buttons = form.Element.Find( "input[type=radio], input[type=checkbox]" ) .GroupBy( element => element.Attribute( "name" ).Value(), StringComparer.OrdinalIgnoreCase ) .Select( group => new FormButtonGroup( form, group.Key, group.ToArray() ) ) .ToArray(); controls = controls.Concat( buttons ); var dunplicate = controls.GroupBy( c => c.Name, StringComparer.OrdinalIgnoreCase ).Where( g => g.Count() > 1 ).FirstOrDefault(); if ( dunplicate != null ) throw new InvalidOperationException( string.Format( "表单中发现多个名为 \"{0}\" 的控件", dunplicate.Key ) ); return controls.ToArray(); }
/// <summary> /// 发现容器中所有可能是控件的元素并包装成控件返回 /// </summary> /// <param name="form">控件所属的表单</param> /// <param name="container">要搜索的容器</param> /// <returns>找到的控件</returns> protected virtual IEnumerable<IFormControl> DiscoveryControls( HtmlForm form, IHtmlContainer container ) { //UNDONE 没有检查 name 是否包含特殊字符 foreach ( var element in container.Elements() ) { if ( inputTextSelector.IsEligible( element ) ) yield return new HtmlInputText( form, element ); else if ( textareaSelector.IsEligible( element ) ) yield return new HtmlTextArea( form, element ); else if ( selectControlSelector.IsEligible( element ) ) yield return new HtmlSelect( form, element ); else { foreach ( var control in DiscoveryControls( form, element ) ) yield return control; } } }
internal HtmlSelect(HtmlForm form, IHtmlElement element) : base(form) { Element = element; options = element.Find("option").Select(e => new HtmlOption(this, e)).ToArray(); }
internal HtmlTextArea(HtmlForm form, IHtmlElement element) : base(form, element) { }
/// <summary> /// 尝试对表单所有元素设置值 /// </summary> /// <param name="form">要设置值的表单</param> /// <param name="values">要设置的值</param> /// <returns>是否成功</returns> public static bool TrySetValues(this HtmlForm form, NameValueCollection values) { throw new NotImplementedException(); }
/// <summary> /// 找到指定字段的错误信息显示容器 /// </summary> /// <param name="form">字段所属的表单</param> /// <param name="fieldName">字段名称</param> /// <returns>错误信息显示容器</returns> protected virtual IHtmlElement FindErrorMessageContainer(HtmlForm form, string fieldName) { return(form.Element.FindFirstOrDefault("#error_" + fieldName) ?? form.Element.FindFirstOrDefault(".error_" + fieldName)); }
/// <summary> /// 通过表单元数据提供程序,针对指定表单创建 FormMeatadata 对象 /// </summary> /// <param name="form">要创建元数据的表单</param> /// <param name="provider">表单元数据提供程序</param> public FormMetadata( HtmlForm form, IFormMetadataProvider provider ) { Form = form; MetadataProvider = provider; }
/// <summary> /// 构建 FormControl 实例 /// </summary> /// <param name="form">控件所属表单</param> protected FormControl( HtmlForm form ) { Form = form; }
/// <summary> /// 创建标准表单控件组实例 /// </summary> /// <param name="form">控件所属表单</param> protected FormGroupControl(HtmlForm form) : base(form) { }
/// <summary> /// 派生类调用此构造函数初始化 FormTextControl 实例 /// </summary> /// <param name="form">所属表单</param> /// <param name="element">定义文本控件的元素</param> protected FormTextControl(HtmlForm form, IHtmlElement element) : base(form) { Element = element; MaxLength = GetMaxLength(); }
/// <summary> /// 构建 FormControl 实例 /// </summary> /// <param name="form">控件所属表单</param> protected FormControl(HtmlForm form) { Form = form; }
/// <summary> /// 对表单执行验证 /// </summary> /// <param name="form">要验证的表单</param> /// <returns>验证结果</returns> public IFormValidationResult ValidateForm(HtmlForm form) { return(new FormValidationResult(form, form.Controls.Select(control => Validators[control.Name].Validate(control.Value)).NotNull())); }
/// <summary> /// 创建标准表单控件组实例 /// </summary> /// <param name="form">控件所属表单</param> protected FormGroupControl( HtmlForm form ) : base( form ) { }
/// <summary> /// 移除没有错误信息的字段的错误信息呈现容器 /// </summary> /// <param name="form">字段所属的表单</param> /// <param name="fieldName">字段名</param> protected virtual void RemoveErrorMessage( HtmlForm form, string fieldName ) { var container = FindErrorMessageContainer( form, fieldName ); if ( container != null ) container.Remove(); }
internal HtmlTextArea( HtmlForm form, IHtmlElement element ) : base( form, element ) { }
/// <summary> /// 找到指定字段的错误信息显示容器 /// </summary> /// <param name="form">字段所属的表单</param> /// <param name="fieldName">字段名称</param> /// <returns>错误信息显示容器</returns> protected virtual IHtmlElement FindErrorMessageContainer( HtmlForm form, string fieldName ) { return form.Element.FindFirstOrDefault( "#error_" + fieldName ) ?? form.Element.FindFirstOrDefault( ".error_" + fieldName ); }
/// <summary> /// 通过表单元数据提供程序,针对指定表单创建 FormMeatadata 对象 /// </summary> /// <param name="form">要创建元数据的表单</param> /// <param name="provider">表单元数据提供程序</param> public FormMetadata(HtmlForm form, IFormMetadataProvider provider) { Form = form; MetadataProvider = provider; }
public GenericMvcFormValidator( HtmlForm form, ModelStateDictionary modelStates ) : base(form, modelStates) { }
/// <summary> /// 对表单执行验证 /// </summary> /// <param name="form">要验证的表单</param> /// <returns>验证结果</returns> public IFormValidationResult ValidateForm( HtmlForm form ) { return new FormValidationResult( form, form.Controls.Select( control => Validators[control.Name].Validate( control.Value ) ).NotNull() ); }
/// <summary> /// 创建 MvcFormValidator 的实例 /// </summary> /// <param name="form">所针对的表单对象</param> /// <param name="modelStates">表单(模型)验证信息</param> public MvcFormValidator( HtmlForm form, ModelStateDictionary modelStates ) : base(form) { _modelStates = modelStates; foreach ( var pair in _modelStates ) { var input = form[pair.Key]; if ( input == null ) continue; var state = pair.Value; AddFieldValidator( CreateFieldValidator( input, state ) ); } }
internal HtmlInputText( HtmlForm form, IHtmlElement element ) : base( form, element ) { var value = element.Attribute( "value" ).Value() ?? ""; }