public void CreateInstanceForProxyWithNullValidatedTypeThrows() { object value = new object(); MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "ObjectProperty", null); ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); }
public void CreateInstanceForProxyWithInvalidValidatedPropertyNameThrows() { object value = new object(); MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "Invalid", typeof(MockValidatedType)); ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); }
/// <summary> /// Does the validate. /// </summary> /// <param name="objectToValidate">The object to validate.</param> /// <param name="currentTarget">The current target.</param> /// <param name="key">The key.</param> /// <param name="validationResults">The validation results.</param> protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults) { Validator validator = new ValidationIntegrationHelper(this).GetValidator(); if (validator != null) { ValidationResults results = validator.Validate(this); if (!results.IsValid) { validationResults.AddAllResults(results); } } }
/// <summary> /// Determines whether the content in the input control is valid. /// </summary> /// <returns><see langword="true"/> if the control is valid; otherwise, <see langword="false"/>.</returns> protected override bool EvaluateIsValid() { Validator validator = new ValidationIntegrationHelper(this).GetValidator(); if (validator != null) { ValidationResults validationResults = validator.Validate(this); this.ErrorMessage = FormatErrorMessage(validationResults, this.DisplayMode); return validationResults.IsValid; } else { this.ErrorMessage = ""; return true; } }
private void OnCellValidating(object source, DataGridViewCellValidatingEventArgs e) { var row = Grid.Rows[e.RowIndex]; if (row.IsNewRow) { return; } var validationProxy = new GridValidationIntegrationProxy(Grid.Columns[e.ColumnIndex].DataPropertyName, e.FormattedValue, this); ValidationIntegrationHelper helper; try { helper = new ValidationIntegrationHelper(validationProxy); } catch (InvalidOperationException) { // ignore missing property return; } var validator = helper.GetValidator(); if (validator == null) { // this property should not be checked return; } var validationResults = validator.Validate(validationProxy); if (!validationResults.IsValid) { var builder = new StringBuilder(); foreach (ValidationResult result in validationResults) { builder.AppendLine(result.Message); } row.Cells[e.ColumnIndex].ErrorText = builder.ToString(); } else { row.Cells[e.ColumnIndex].ErrorText = string.Empty; } }
/// <summary> /// Determines whether the content in the input control is valid. /// </summary> /// <returns><see langword="true"/> if the control is valid; otherwise, <see langword="false"/>.</returns> protected override bool EvaluateIsValid() { Validator validator = new ValidationIntegrationHelper(this).GetValidator(); if (validator != null) { ValidationResults validationResults = validator.Validate(this); this.ErrorMessage = FormatErrorMessage(validationResults, this.DisplayMode); return(validationResults.IsValid); } else { this.ErrorMessage = ""; return(true); } }
public override System.Windows.Controls.ValidationResult Validate(object value, CultureInfo cultureInfo) { this.value = value; Validator validator = new ValidationIntegrationHelper(this).GetValidator(); if (validator != null) { ValidationResults validationResults = validator.Validate(this); string errorMessage = FormatErrorMessage(validationResults); return(new System.Windows.Controls.ValidationResult(validationResults.IsValid, errorMessage)); } else { return(new System.Windows.Controls.ValidationResult(true, null)); } }
public void CanGetObjectValueFromIntegrationProxy() { object value = new object(); MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "ObjectProperty", typeof(MockValidatedType)); ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); object retrievedValue; string valueRetrievalFailureMessage; bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage); Assert.IsTrue(status); Assert.AreSame(value, retrievedValue); Assert.AreEqual(null, valueRetrievalFailureMessage); }
public void GetValueConvertedWithDefaultConversionReturnsFailureIfConversionIsNotPossible() { object value = "00012345abc"; MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "IntProperty", typeof(MockValidatedType)); ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); object retrievedValue; string valueRetrievalFailureMessage; bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage); Assert.IsFalse(status); Assert.AreEqual(null, retrievedValue); Assert.IsTrue(TemplateStringTester.IsMatch(Resources.ErrorCannotPerfomDefaultConversion, valueRetrievalFailureMessage)); Assert.AreEqual(null, originalConvertedValue); Assert.AreEqual(null, valueToConvert); }
public void CanGetValueConvertedWithDefaultConversionFromNullValueIfNoEventHandlerSet() { object value = null; MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "ObjectProperty", typeof(MockValidatedType)); ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); object retrievedValue; string valueRetrievalFailureMessage; bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage); Assert.IsTrue(status); Assert.AreEqual(null, retrievedValue); Assert.AreEqual(null, valueRetrievalFailureMessage); Assert.AreEqual(null, originalConvertedValue); Assert.AreEqual(null, valueToConvert); }
/// <summary> /// Validate property. /// </summary> /// <param name="propName">Property name.</param> /// <returns>Error string or null.</returns> private string _ValidateProperty(string propName) { string error = null; if (_CanValidate(propName)) { _validationProxy.ValidatedPropertyName = propName; ValidationIntegrationHelper hlp = new ValidationIntegrationHelper( _validationProxy); Validator validator = hlp.GetValidator(); ValidationResults res = validator.Validate(this); if (!res.IsValid) { error = _FindError(res, propName); } } return(error); }
/// <summary> /// Creates validators for objects of the specified type and it's properties. /// </summary> /// <param name="objectType">The type of objects to create validators for.</param> /// <returns>An instance of the validators container with validators for the specified /// type.</returns> private static ValidatorsContainer _CreateValidators(Type objectType) { Debug.Assert(objectType != null); var propertyFlags = BindingFlags.Instance | BindingFlags.Public; var validateableProperties = from propertyInfo in objectType.GetProperties(propertyFlags) where propertyInfo.GetCustomAttributes <BaseValidationAttribute>().Any() select propertyInfo; var propertyValidators = new Dictionary <string, Validator>(); foreach (var propertyInfo in validateableProperties) { var validationProxy = new PropertyValidationProxy { ProvidesCustomValueConversion = false, SpecificationSource = ValidationSpecificationSource.Attributes, ValidatedType = objectType, Ruleset = string.Empty, ValidatedPropertyName = propertyInfo.Name, }; var validationHelper = new ValidationIntegrationHelper(validationProxy); var validator = validationHelper.GetValidator(); propertyValidators.Add(propertyInfo.Name, validator); } var objectValidator = ValidationFactory.CreateValidator(objectType); return(new ValidatorsContainer { ObjectValidator = objectValidator, PropertyValidators = propertyValidators, }); }
public void CanBuildValidatorForProperty() { object value = new object(); MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "ObjectProperty", typeof(MockValidatedType)); integrationProxy.ValueConvertEvent += this.OnValueConvertWithFailure; ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); Validator validator = integrationHelper.GetValidator(); //Assert.IsNotNull(validator); //MockValidatedType instance = new MockValidatedType(); //ValidationResults validationResults = validator.Validate(instance); //Assert.IsFalse(validationResults.IsValid); //IList<ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults); //Assert.AreEqual(1, resultsList.Count); //Assert.AreEqual("object property message", resultsList[0].Message); }
public void GetValueConvertedThroughEventSettingFailureMessageFromIntegrationProxyReturnsFailure() { object value = new object(); MockIntegrationProxy integrationProxy = new MockIntegrationProxy(value, "", ValidationSpecificationSource.Attributes, "ObjectProperty", typeof(MockValidatedType)); integrationProxy.ValueConvertEvent += this.OnValueConvertWithFailure; ValidationIntegrationHelper integrationHelper = new ValidationIntegrationHelper(integrationProxy); object retrievedValue; string valueRetrievalFailureMessage; bool status = integrationHelper.GetValue(out retrievedValue, out valueRetrievalFailureMessage); Assert.IsFalse(status); Assert.AreEqual(null, retrievedValue); Assert.AreEqual(conversionErrorMessage, valueRetrievalFailureMessage); Assert.AreSame(value, originalConvertedValue); Assert.AreSame(value, valueToConvert); }
internal bool GetValue(out object value, out string failureMessage) { ValidationIntegrationHelper helper = new ValidationIntegrationHelper(this); return(helper.GetValue(out value, out failureMessage)); }
public bool GetValue(out object value, out string failureMessage) { ValidationIntegrationHelper helper = new ValidationIntegrationHelper(this); return helper.GetValue(out value, out failureMessage); }
internal bool GetValue(out object value, out string valueAccessFailureMessage) { ValidationIntegrationHelper helper = new ValidationIntegrationHelper(this); return helper.GetValue(out value, out valueAccessFailureMessage); }