/// <summary> /// 尝试查找最小包含指定输入控件的容器 /// </summary> /// <param name="inputControl">输入控件</param> /// <returns>找到的最小包含容器</returns> public static IHtmlContainer FindContainer(this IHtmlInputControl inputControl) { var inputText = inputControl as HtmlInputText; if (inputText != null) { return(inputText.Element.Container); } var textarea = inputControl as HtmlTextArea; if (textarea != null) { return(textarea.Element.Container); } var select = inputControl as HtmlSelect; if (select != null) { return(select.Element.Container); } var group = inputControl as HtmlButtonGroup; if (group != null) { return(FindContainer(group)); } throw new NotSupportedException(); }
/// <summary> /// 获取指定控件在客户端设置的值 /// </summary> /// <param name="inputer">输入控件</param> /// <returns></returns> public static string SubmittedValue(this IHtmlInputControl inputer) { if (inputer == null) { throw new ArgumentNullException("inputer"); } var data = inputer.Form.SubmittedValues; if (data == null) { return(null); } return(data[inputer.Name]); }
/// <summary> /// 获取字符串形式表达的 Value 值 /// </summary> /// <param name="input">输入控件</param> /// <returns></returns> public static string ValueString(this IHtmlInputControl input) { var textInput = input as HtmlInputText; if (textInput != null) { return(textInput.TextValue); } var group = input as IHtmlGroupControl; if (group != null) { return(string.Join(",", group.CurrentValues().ToArray())); } throw new NotSupportedException(); }
/// <summary> /// 尝试为输入控件设置值 /// </summary> /// <param name="input">输入控件</param> /// <param name="value">要设置的值</param> /// <returns>是否成功</returns> /// <remarks>对于密码框或不存在值吻合选项的输入组,此方法会设置失败并返回false</remarks> /// <exception cref="System.NotSupportedException">不被支持的输入控件</exception> public static bool TrySetValue(this IHtmlInputControl input, string value) { var textInput = input as HtmlInputText; if (textInput != null) { return(TrySetValue(textInput, value)); } var group = input as IHtmlGroupControl; if (group != null) { return(TrySetValue(group, value)); } throw new NotSupportedException(); }
/// <summary> /// 尝试获取字段名 /// </summary> /// <param name="input">输入控件</param> /// <returns></returns> protected virtual string GetFieldName(IHtmlInputControl input) { if (input == null) { throw new ArgumentNullException("input"); } var labels = input.Labels(); if (labels.Length == 1) { return(labels[0].Text); } else { return(input.Name); } }
private IHtmlElement GetInputElement(IHtmlInputControl input) { var inputText = input as HtmlInputText; if (inputText != null) { return(inputText.Element); } var select = input as HtmlSelect; if (select != null) { return(select.Element); } var textaera = input as HtmlTextArea; return(textaera.Element); }
protected override IHtmlElement FailedMessageContainer(IHtmlInputControl input) { if (InputControlFinder != null) { return(InputControlFinder(input)); } if (InputElementFinder != null) { var element = GetInputElement(input); if (element == null) { return(null); } return(InputElementFinder(element)); } return(base.FailedMessageContainer(input)); }
/// <summary> /// 将客户端提交来的值,应用到对应的输入控件上 /// </summary> /// <param name="control">输入控件</param> public static IHtmlInputControl ApplySubmittedValue(this IHtmlInputControl control) { if (control == null) { throw new ArgumentNullException("control"); } var textControl = control as IHtmlTextControl; if (textControl != null) { ApplySubmittedValue(textControl); } var groupControl = control as IHtmlGroupControl; if (groupControl != null) { ApplySubmittedValue(groupControl); } return(control); }
/// <summary> /// 添加一个字段验证规则 /// </summary> /// <param name="input">输入控件</param> /// <param name="fieldName">字段名</param> /// <param name="validators">值验证器</param> protected void AddFieldValidation(IHtmlInputControl input, string fieldName, params IHtmlValueValidator[] validators) { if (input == null) { throw new ArgumentNullException("input"); } if (!input.Form.Equals(Form)) { throw new ArgumentException("输入控件不属于验证表单", "input"); } if (fieldName == null) { fieldName = GetFieldName(input); } var fieldValidator = new GenericFieldValidator(input, fieldName, validators); AddFieldValidator(fieldValidator); }
/// <summary> /// 尝试为输入控件设置值 /// </summary> /// <param name="input">输入控件</param> /// <param name="value">要设置的值</param> /// <returns>是否成功</returns> /// <remarks>对于密码框或不存在值吻合选项的输入组,此方法会设置失败并返回false</remarks> /// <exception cref="System.NotSupportedException">不被支持的输入控件</exception> public static bool TrySetValue(this IHtmlInputControl input, string value) { if (input == null) { return(false); } var textControl = input as IHtmlTextControl; if (textControl != null) { return(TrySetValue(textControl, value)); } var group = input as IHtmlGroupControl; if (group != null) { return(TrySetValue(group, value)); } throw new NotSupportedException(string.Format("名为 \"{0}\" 输入控件的类型 \"{1}\" 不受支持。", input.Name, input.GetType().FullName)); }
protected override IHtmlElement FailedMessageContainer( IHtmlInputControl input ) { if ( InputControlFinder != null ) return InputControlFinder( input ); if ( InputElementFinder != null ) { var element = GetInputElement( input ); if ( element == null ) return null; return InputElementFinder( element ); } return base.FailedMessageContainer( input ); }
public GenericFieldValidator(IHtmlInputControl inputControl, string fieldName, params IHtmlValueValidator[] validators) : base(inputControl, fieldName) { _validators = validators; }
/// <summary> /// 获取验证失败信息需要呈现的容器,默认实现查找 errorFor 属性值等于输入控件名的元素 /// </summary> /// <param name="input">输入控件</param> /// <returns>针对此输入控件的错误提示容器</returns> protected override IHtmlElement FailedMessageContainer(IHtmlInputControl input) { return(Form.Element.Find(string.Format("[errorfor={0}]", input.Name)).FirstOrDefault()); }
/// <summary> /// 派生类重写此方法查找显示指定输入控件验证描述信息的容器 /// </summary> /// <param name="input">要显示描述信息的输入控件</param> /// <returns>显示描述信息的容器</returns> protected virtual IHtmlElement FieldDescrptionContainer(IHtmlInputControl input) { return(null); }
/// <summary> /// 派生类重写此方法查找显示指定输入控件验证错误信息的容器 /// </summary> /// <param name="input">要显示错误信息的输入控件</param> /// <returns>显示错误信息的容器</returns> protected virtual IHtmlElement FailedMessageContainer(IHtmlInputControl input) { return(null); }
/// <summary> /// 创建 MvcFieldValidator 对象 /// </summary> /// <param name="input"></param> /// <param name="state"></param> public MvcFieldValidator(IHtmlInputControl input, ModelState state) { _state = state; _input = input; }
/// <summary> /// 派生类重写此方法创建字段验证器 /// </summary> /// <param name="input">输入控件</param> /// <param name="state">模型验证信息</param> /// <returns>创建的字段验证器</returns> protected virtual IHtmlFieldValidator CreateFieldValidator( IHtmlInputControl input, ModelState state ) { return new MvcFieldValidator( input, state ); }
/// <summary> /// 派生类重写此方法创建字段验证器 /// </summary> /// <param name="input">输入控件</param> /// <param name="state">模型验证信息</param> /// <returns>创建的字段验证器</returns> protected virtual IHtmlFieldValidator CreateFieldValidator(IHtmlInputControl input, ModelState state) { return(new MvcFieldValidator(input, state)); }
/// <summary> /// 派生类重写此方法查找显示指定输入控件验证错误信息的容器 /// </summary> /// <param name="input">要显示错误信息的输入控件</param> /// <returns>显示错误信息的容器</returns> protected virtual IHtmlElement FailedMessageContainer( IHtmlInputControl input ) { return null; }
/// <summary> /// 添加一个字段验证规则 /// </summary> /// <param name="input">输入控件</param> /// <param name="fieldName">字段名</param> /// <param name="validators">值验证器</param> protected void AddFieldValidation( IHtmlInputControl input, string fieldName, params IHtmlValueValidator[] validators ) { if ( input == null ) throw new ArgumentNullException( "input" ); if ( !input.Form.Equals( Form ) ) throw new ArgumentException( "输入控件不属于验证表单", "input" ); if ( fieldName == null ) fieldName = GetFieldName( input ); var fieldValidator = new GenericFieldValidator( input, fieldName, validators ); AddFieldValidator( fieldValidator ); }
/// <summary> /// 创建 MvcFieldValidator 对象 /// </summary> /// <param name="input"></param> /// <param name="state"></param> public MvcFieldValidator( IHtmlInputControl input, ModelState state ) { _state = state; _input = input; }
/// <summary> /// 获取验证失败信息需要呈现的容器,默认实现查找 errorFor 属性值等于输入控件名的元素 /// </summary> /// <param name="input">输入控件</param> /// <returns>针对此输入控件的错误提示容器</returns> protected override IHtmlElement FailedMessageContainer( IHtmlInputControl input ) { return Form.Element.Find( string.Format( "[errorfor={0}]", input.Name ) ).FirstOrDefault(); }
/// <summary> /// 派生类重写此方法查找显示指定输入控件验证描述信息的容器 /// </summary> /// <param name="input">要显示描述信息的输入控件</param> /// <returns>显示描述信息的容器</returns> protected virtual IHtmlElement FieldDescrptionContainer( IHtmlInputControl input ) { return null; }
protected HtmlFieldValidator(IHtmlInputControl inputControl, string fieldName) { _input = inputControl; _fieldName = fieldName; }
/// <summary> /// 尝试获取字段名 /// </summary> /// <param name="input">输入控件</param> /// <returns></returns> protected virtual string GetFieldName( IHtmlInputControl input ) { if ( input == null ) throw new ArgumentNullException( "input" ); var labels = input.Labels(); if ( labels.Length == 1 ) return labels[0].Text; else return input.Name; }
public GenericFieldValidator( IHtmlInputControl inputControl, string fieldName, params IHtmlValueValidator[] validators ) : base(inputControl, fieldName) { _validators = validators; }
private IHtmlElement GetInputElement( IHtmlInputControl input ) { var inputText = input as HtmlInputText; if ( inputText != null ) return inputText.Element; var select = input as HtmlSelect; if ( select != null ) return select.Element; var textaera = input as HtmlTextArea; return textaera.Element; }
protected HtmlFieldValidator( IHtmlInputControl inputControl, string fieldName ) { _input = inputControl; _fieldName = fieldName; }