/// <summary> /// Retrieves a value from <paramref name="source"/>. /// </summary> /// <param name="source">The source for the value.</param> /// <param name="value">The value retrieved from the <paramref name="source"/>.</param> /// <param name="valueAccessFailureMessage">A message describing the failure to access the value, if any.</param> /// <returns><see langword="true"/> when the retrieval was successful; <see langword="false"/> otherwise.</returns> /// <remarks>Subclasses provide concrete value accessing behaviors.</remarks> public override bool GetValue(object source, out object value, out string valueAccessFailureMessage) { value = null; valueAccessFailureMessage = null; ValidatedControlItem validatedControlItem = source as ValidatedControlItem; if (validatedControlItem == null) { throw new InvalidOperationException(Resources.ExceptionValueAccessRequiresValidatedControlItem); } if (!propertyName.Equals(validatedControlItem.SourcePropertyName)) { validatedControlItem = validatedControlItem.ValidationProvider.GetExistingValidatedControlItem(propertyName); if (validatedControlItem == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.ExceptionValueAccessPropertyNotFound, propertyName)); } } return(validatedControlItem.GetValue(out value, out valueAccessFailureMessage)); }
/// <summary> /// Sets the name of the property to use when extracting the value from <paramref name="control"/>. /// </summary> /// <param name="control">The <see cref="Control"/> to configure.</param> /// <param name="validatedPropertyName">The name of the property.</param> /// <exception cref="ArgumentNullException">when <paramref name="control"/> is null.</exception> public void SetValidatedProperty(Control control, string validatedPropertyName) { ValidatedControlItem validatedControlItem = this.EnsureValidatedControlItem(control); validatedControlItem.ValidatedPropertyName = validatedPropertyName; ClearValidation(validatedControlItem); }
/// <summary> /// Invokes the validation process for a control. /// </summary> /// <remarks> /// This method allows for programmatic invocation of the validation process. The control needs to be already /// configured for validation with the validation provider through the extender properties. /// </remarks> /// <param name="validatedControlItem">The validated control to validate.</param> /// <exception cref="ArgumentNullException">when <paramref name="validatedControlItem"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">when the <see cref="ValidationProvider.SourceTypeName"/> has not /// been specified or is invalid.</exception> /// <exception cref="InvalidOperationException">when the source property name for the control to validate has not /// been specified or is invalid.</exception> public void PerformValidation(ValidatedControlItem validatedControlItem) { if (enabled) { if (validatedControlItem == null) { throw new ArgumentNullException("validatedControlItem"); } Validator validator = validatedControlItem.Validator; if (validator != null) { ValidationResults validationResults = validator.Validate(validatedControlItem); validatedControlItem.IsValid = validationResults.IsValid; if (errorProvider != null) { string errorProviderMessage = null; if (!validationResults.IsValid) { errorProviderMessage = FormatErrorMessage(validationResults); } errorProvider.SetError(validatedControlItem.Control, errorProviderMessage); } if (ValidationPerformed != null) { ValidationPerformed(this, new ValidationPerformedEventArgs(validatedControlItem.Control, validationResults)); } } } }
/// <summary> /// Sets the name of the property on the type specified by <see cref="ValidationProvider.SourceTypeName"/> for which /// the validation specification should be retrieved to validate the value for <paramref name="control"/>. /// </summary> /// <param name="control">The <see cref="Control"/> to configure.</param> /// <param name="sourcePropertyName">The property name.</param> /// <exception cref="ArgumentNullException">when <paramref name="control"/> is null.</exception> public void SetSourcePropertyName(Control control, string sourcePropertyName) { ValidatedControlItem validatedControlItem = this.EnsureValidatedControlItem(control); validatedControlItem.SourcePropertyName = sourcePropertyName; ClearValidation(validatedControlItem); }
/// <summary> /// Sets the indication of whether automatic validation should be performed for /// <paramref name="control"/> when it fires the <see cref="Control.Validating"/> event. /// </summary> /// <param name="control">The <see cref="Control"/> to configure.</param> /// <param name="performValidation"><see langword="true"/> if the control should be automatically validated; /// otherwise, <see langword="false"/>.</param> /// <exception cref="ArgumentNullException">when <paramref name="control"/> is null.</exception> public void SetPerformValidation(Control control, bool performValidation) { ValidatedControlItem validatedControlItem = this.EnsureValidatedControlItem(control); validatedControlItem.PerformValidation = performValidation; ClearValidation(validatedControlItem); }
internal void PerformValidation(ValidatedControlItem validatedControlItem) { if (this.enabled) { Validator validator = validatedControlItem.Validator; if (validator != null) { ValidationResults validationResults = validator.Validate(validatedControlItem); validatedControlItem.IsValid = validationResults.IsValid; if (this.errorProvider != null) { string errorProviderMessage = null; if (!validationResults.IsValid) { errorProviderMessage = FormatErrorMessage(validationResults); } this.errorProvider.SetError(validatedControlItem.Control, errorProviderMessage); } if (this.ValidationPerformed != null) { this.ValidationPerformed(this, new ValidationPerformedEventArgs(validatedControlItem.Control, validationResults)); } } } }
internal void ClearValidation(ValidatedControlItem validatedControlItem) { if (!DesignMode) { if (errorProvider != null) { errorProvider.SetError(validatedControlItem.Control, null); } validatedControlItem.ClearValidation(); } }
/// <summary> /// Invokes the validation process for a control. /// </summary> /// <remarks> /// This method allows for programmatic invocation of the validation process. The control needs to be already /// configured for validation with the validation provider through the extender properties. /// </remarks> /// <param name="control">The control to validate.</param> /// <exception cref="ArgumentNullException">when <paramref name="control"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException">when <paramref name="control"/> is not registered with the validation provider /// by specifying the extended properties.</exception> /// <exception cref="InvalidOperationException">when the <see cref="ValidationProvider.SourceTypeName"/> has not /// been specified or is invalid.</exception> /// <exception cref="InvalidOperationException">when the source property name for the control to validate has not /// been specified or is invalid.</exception> public void PerformValidation(Control control) { if (control == null) { throw new ArgumentNullException("control"); } ValidatedControlItem existingValidatedControlItem = GetExistingValidatedControlItem(control); if (existingValidatedControlItem == null) { throw new ArgumentException(Resources.ExceptionControlNotExtended, "control"); } PerformValidation(existingValidatedControlItem); }
internal ValidatedControlItem EnsureValidatedControlItem(Control control) { if (control == null) { throw new ArgumentNullException("control"); } ValidatedControlItem existingItem = (ValidatedControlItem)items[control]; if (existingItem == null) { existingItem = new ValidatedControlItem(this, control); items[control] = existingItem; } return(existingItem); }
internal ValidatedControlItem EnsureValidatedControlItem(Control control) { if (control == null) { throw new ArgumentNullException("control"); } ValidatedControlItem existingItem = (ValidatedControlItem)this.items[control]; if (existingItem == null) { existingItem = new ValidatedControlItem(this, control); this.items[control] = existingItem; } return existingItem; }
internal void ClearValidation(ValidatedControlItem validatedControlItem) { if (!this.DesignMode) { if (this.errorProvider != null) { this.errorProvider.SetError(validatedControlItem.Control, null); } validatedControlItem.ClearValidation(); } }