public async Task UpdateSiteInfoAsync() { if (string.IsNullOrWhiteSpace(ApiEndpoint)) { _ErrorsContainer.SetErrors(nameof(ApiEndpoint), Tx.T("errors.field is required")); return; } if (IsBusy) { return; } IsBusy = true; Status = Tx.T("wiki site.validating api endpoint"); var urlToValidate = ApiEndpoint; try { // Search for API endpoint URL var endPoint = await Site.SearchApiEndpointAsync(_SessionService.WikiClient, urlToValidate); if (endPoint == null) { _ErrorsContainer.SetErrors(nameof(ApiEndpoint), Tx.T("errors.invalid api endpoint")); Status = Tx.T("errors.invalid api endpoint"); return; } Status = Tx.T("please wait"); // Gather site information var site = await _SessionService.CreateSiteAsync(endPoint); SiteName = site.SiteInfo.SiteName; UserName = site.UserInfo.Name; // Clear validation errors _ErrorsContainer.ClearErrors(null); _ErrorsContainer.ClearErrors(nameof(ApiEndpoint)); Status = null; if (urlToValidate == ApiEndpoint) { SetValidatedApiEndpoint(endPoint); _NeedValidateApiEndpoint = false; } } catch (Exception ex) { Status = Utility.GetExceptionMessage(ex); _ErrorsContainer.SetErrors(null, ex.Message); } finally { IsBusy = false; } }
public void WhenGettingErrors_ThenErrorsPerPropertyReturned() { List <string> validatedProperties = new(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validation.SetErrors("property2", new[] { "message" }); var errors = validation.GetErrors(); Assert.True(errors.Any()); Assert.True(errors.Count == 2); Assert.Contains(errors, e => e.Key.Equals("property1")); Assert.Contains(errors, e => e.Key.Equals("property2")); }
public void WhenClearingErrorsForPropertyWithErrors_ThenPropertyHasNoErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validation.SetErrors("property2", new[] { "message2" }); validation.ClearErrors("property1"); Assert.IsTrue(validation.HasErrors); Assert.IsFalse(validation.GetErrors("property1").Any()); Assert.IsTrue(validation.GetErrors("property2").Any()); }
public void WhenClearingErrorsForPropertyWithErrors_ThenPropertyHasNoErrors() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validation.SetErrors("property2", new[] { "message2" }); validation.ClearErrors("property1"); Assert.True(validation.HasErrors); Assert.False(validation.GetErrors("property1").Any()); Assert.True(validation.GetErrors("property2").Any()); }
public void WhenSettingNoErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasNoErrors() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validatedProperties.Clear(); validation.SetErrors("property1", new string[0]); Assert.IsFalse(validation.HasErrors); Assert.IsFalse(validation.GetErrors("property1").Any()); CollectionAssert.AreEqual(new[] { "property1" }, validatedProperties); }
protected override void Validate() { List <string> errors = new List <string>(); if (IsSelected && !IsValid) { switch (TypeName.ToLower()) { case "string": errors.Add(string.Format("{0}\ncannot\nbe empty!", FieldName)); break; case "int32": errors.Add(string.Format("\"{0}\"\nis not\nvalid!", Value)); break; case "sbyte": errors.Add(string.Format("\"{0}\"\nis not\nvalid!", Value)); break; case "datetime": errors.Add(string.Format("\"{0}\"\nis not\na valid date!", Value)); break; default: errors.Add(string.Format("\"{0}\"\nis not\na valid!", Value)); break; } } ErrorsContainer.SetErrors(nameof(Value), errors); OnErrorsChanged(nameof(Value)); _eventAggregator.GetEvent <UpdateMessage>().Publish(true); }
private bool ValidateProperty <T>(T value, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } var propertyInfo = this.GetType().GetRuntimeProperty(propertyName); if (propertyInfo == null) { throw new ArgumentException("Invalid property name", propertyName); } var propertyErrors = new List <ValidationResult>(); Validator.TryValidateProperty(value, new ValidationContext(this) { MemberName = propertyName }, propertyErrors); var errors = Enumerable.ToList <string>(propertyErrors.Select(validationResult => validationResult.ErrorMessage)); _errorsContainer.SetErrors(propertyInfo.Name, errors); return(errors.Count > 0); }
public void WhenSettingErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasErrors() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validatedProperties.Clear(); validation.SetErrors("property1", new[] { "message" }); Assert.True(validation.HasErrors); Assert.Contains("message", validation.GetErrors("property1")); Assert.Equal(new[] { "property1" }, validatedProperties); }
void Validate() { if (Validator != null) { lock (lockObj) { ValidationResult results = Validator.Validate(); if (results != null) { // reset error container ErrorsContainer.ClearAllErrors(); List <string> errorPropertyNames = results.Errors.Select(x => x.PropertyName).ToList(); if ((errorPropertyNames != null) && (errorPropertyNames.Count > 0)) { foreach (var propertyName in errorPropertyNames) { List <string> errorMessages = results.Errors.Where(pn => pn.PropertyName.ToLower() == propertyName.ToLower()).Select(x => x.ErrorMessage).ToList(); ErrorsContainer.SetErrors(propertyName, errorMessages); OnErrorsChanged(propertyName); } } } } } }
public void WhenSettingErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validatedProperties.Clear(); validation.SetErrors("property1", new[] { "message" }); Assert.IsTrue(validation.HasErrors); Assert.IsTrue(validation.GetErrors("property1").Contains("message")); CollectionAssert.AreEqual(new[] { "property1" }, validatedProperties); }
public void WhenSettingNoErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasNoErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validatedProperties.Clear(); validation.SetErrors("property1", new string[0]); Assert.False(validation.HasErrors); Assert.False(validation.GetErrors("property1").Any()); Assert.Equal(new[] { "property1" }, validatedProperties); }
public void InsertSame(string property) { var called = 0; var container = new ErrorsContainer <int>(_ => called++); container.SetErrors(property, new[] { 1, 2, 4 }); container.SetErrors(property, new[] { 1, 2, 4 }); called .Should().Be(2); container.HasErrors .Should().BeTrue(); container.GetErrors(property) .Should().BeEquivalentTo(new[] { 1, 2, 4 }); }
public void InsertSingleAndRemove(string property) { var called = 0; var container = new ErrorsContainer <int>(_ => called++); container.SetErrors(property, new[] { 1 }); container.SetErrors(property, null); called .Should().Be(2); container.HasErrors .Should().BeFalse(); container.GetErrors(property) .Should().BeEmpty(); }
public void WhenSettingErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasErrors() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <ValidationResult>(pn => validatedProperties.Add(pn)); var validationError = new ValidationResult("message", new[] { "property1" }); validation.SetErrors("property1", new[] { validationError }); validatedProperties.Clear(); validation.SetErrors("property1", new[] { validationError }); Assert.IsTrue(validation.HasErrors); Assert.IsTrue(validation.GetErrors("property1").Contains(validationError)); CollectionAssert.AreEqual(new[] { "property1" }, validatedProperties); }
public void ValidateObject() { ErrorsContainer.ClearErrors(""); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(this, new ValidationContext(this, null, null), validationResults); ErrorsContainer.SetErrors("", validationResults); }
public static void SetErrors <T>(this ErrorsContainer <T> errorsContainer, string propertyName, params T[] errors) { if (errorsContainer == null) { throw new ArgumentNullException(nameof(errorsContainer)); } errorsContainer.SetErrors(propertyName, (IEnumerable <T>)errors); }
public EditWaypointViewModelDesign() : base(null) { WaypointModel = new WaypointModel() { Identifier = "IDENT", Latitude = 6.66, Longitude = 12.89 }; ErrorsContainer.SetErrors(() => Identifier, new[] { new ValidationResult(false, "Identifier error") }); ErrorsContainer.SetErrors(() => Longitude, new [] { new ValidationResult(false, "Longitude error") }); ErrorsContainer.SetErrors(() => Latitude, new[] { new ValidationResult(false, "Latitude error") }); }
public void InsertForTwoProperties() { var called = 0; var container = new ErrorsContainer <int>(_ => called++); container.SetErrors(TestProperty1, new[] { 1, 2, 4 }); container.SetErrors(TestProperty2, new[] { 5, 6 }); called .Should().Be(2); container.HasErrors .Should().BeTrue(); container.GetErrors(TestProperty1) .Should().BeEquivalentTo(new[] { 1, 2, 4 }); container.GetErrors(TestProperty2) .Should().BeEquivalentTo(new[] { 5, 6 }); }
protected virtual void Submit() { ErrorsContainer.ClearErrors(); if (string.IsNullOrEmpty(Name)) { ErrorsContainer.SetErrors(nameof(Name), new List <string> { "Please Input Username" }); } }
public void WhenClearingErrorsWithNullPropertyName_ThenHasErrors() { List <string> validatedProperties = new(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validation.ClearErrors(null); Assert.True(validation.HasErrors); Assert.True(validation.GetErrors("property1").Any()); }
private void ValidateProperty(object value, [CallerMemberName] string propertyName = null) { var res = new List <ValidationResult>(); var validationresult = Validator.TryValidateProperty(value, new ValidationContext(this, null, null) { MemberName = propertyName }, res); ErrorsContainer.SetErrors(propertyName, res); HasErrors = validationresult; }
public void WhenSettingNoErrorsForPropertyWithNoErrors_ThenDoesNotNotifyChangesAndHasNoErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<ValidationResult>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new ValidationResult[0]); Assert.IsFalse(validation.HasErrors); Assert.IsFalse(validation.GetErrors("property1").Any()); Assert.IsFalse(validatedProperties.Any()); }
public void WhenGettingErrorsWithPropertyName_ThenErrorsReturned() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); var errors = validation.GetErrors("property1"); Assert.True(errors.Any()); }
public void WhenSettingNoErrorsForPropertyWithNoErrors_ThenDoesNotNotifyChangesAndHasNoErrors() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new string[0]); Assert.IsFalse(validation.HasErrors); Assert.IsFalse(validation.GetErrors("property1").Any()); Assert.IsFalse(validatedProperties.Any()); }
public void WhenGettingErrorsWithNullPropertyName_ThenNoErrorsReturned() { List <string> validatedProperties = new List <string>(); var validation = new ErrorsContainer <string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); var errors = validation.GetErrors(null); Assert.True(validation.HasErrors); Assert.True(errors.Count() == 0); }
public void WhenGettingErrorsWithNullPropertyName_ThenNoErrorsReturned() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); var errors = validation.GetErrors(null); Assert.IsTrue(validation.HasErrors); Assert.IsTrue(errors.Count() == 0); }
/// <summary> /// Checks if a property already matches a desired value. Sets the property, /// validates its value via a delegate and error message for an invalid value /// and notifies event listeners only when necessary. /// </summary> /// <param name="validationDelegate">A delegate for performing validation of the property value.</param> /// <param name="validationErrorMessage"> /// A <see cref="string"/> containing the error message to set for an invalid property value. /// </param> /// <inheritdoc cref="BindableBase.SetProperty{T}(ref T, T, string)"/> protected virtual bool SetProperty <T>(ref T storage, T value, Func <T, bool> validationDelegate, string validationErrorMessage, [CallerMemberName] string propertyName = null) { if (validationDelegate == null) { throw new ArgumentNullException(nameof(validationDelegate)); } if (!base.SetProperty(ref storage, value, propertyName)) { // No change to property value as it was equal to the desired value. // Therefore no need to (re)validate. return(false); } bool isValid = validationDelegate(value); string[] propertyErrors = !isValid ? new[] { validationErrorMessage } : Array.Empty <string>(); _errorsContainer.SetErrors(propertyName, propertyErrors); // Property set and validated. return(true); }
public void WhenClearingErrorsWithNullPropertyname_ThenHasErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); validation.ClearErrors(null); Assert.IsTrue(validation.HasErrors); Assert.IsTrue(validation.GetErrors("property1").Any()); }
/// <summary> /// Validates <paramref name="value"/> as the value for the property specified by /// <paramref name="validationContext"/> using data annotations validation attributes. /// </summary> /// <param name="validationContext">The context for the validation.</param> /// <param name="value">The value for the property.</param> protected virtual void ValidateProperty(ValidationContext validationContext, object value) { if (validationContext == null) { throw new ArgumentNullException("validationContext"); } var validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(value, validationContext, validationResults); ErrorsContainer.SetErrors(validationContext.MemberName, validationResults); }
private void DoValidate(string propertyName = null) { if (string.IsNullOrWhiteSpace(propertyName)) { foreach (var prop in _propertyNames) { IEnumerable <string> errors = ValidateProperty(prop); ErrorsContainer.SetErrors(prop, errors); } } else { IEnumerable <string> errors = ValidateProperty(propertyName); ErrorsContainer.SetErrors(propertyName, errors); } }
public void InsertNull(string property) { var called = 0; var container = new ErrorsContainer <int>(_ => called++); container.SetErrors(property, null); container.HasErrors .Should().BeFalse(); container.GetErrors(property) .Should().BeEmpty(); called .Should().Be(0); }
protected virtual void ValidateProperty(object value, [CallerMemberName] string propertyName = null) { var context = new ValidationContext(this) { MemberName = propertyName }; var validationErrors = new List <ValidationResult>(); if (!Validator.TryValidateProperty(value, context, validationErrors)) { IEnumerable <string> errors = validationErrors.Select(x => x.ErrorMessage); _errorsContainer.SetErrors(propertyName, errors); } else { _errorsContainer.ClearErrors(propertyName); } }
public override bool ValidateProperties() { base.ValidateProperties(); //Aggregate Invariants var results = new List <ValidationResult>(); var context = new ValidationContext(this); var propertyErrors = new List <string>(); Validator.TryValidateObject(this, context, results); if (results.Any()) { propertyErrors.AddRange(results.Select(c => c.ErrorMessage)); } ErrorsContainer.SetErrors(nameof(Details), propertyErrors); return(ErrorsContainer.HasErrors); }
/// <summary> /// 検証 /// </summary> /// <remarks> /// ValidateAllObjectsは、ICommandのCanExecuteで呼ぶものです。これを呼ばないと、画面表示時の初回判定を行ってくれません。 /// </remarks> /// <returns></returns> internal bool ValidateAllObjects() { if (!this.HasErrors) { var context = new ValidationContext(this); var validationErrors = new List <ValidationResult>(); if (Validator.TryValidateObject(this, context, validationErrors)) { return(true); } var errors = validationErrors.Where(_ => _.MemberNames.Any()).GroupBy(_ => _.MemberNames.First()); foreach (var error in errors) { _errors.SetErrors(error.Key, error.Select(_ => _.ErrorMessage)); } } return(false); }
private bool ValidateLatitude(string val) { var valid = _latConverter.ValidateString(val); if (valid) { ErrorsContainer.ClearErrors(() => Latitude); } else { ErrorsContainer.ClearErrors(() => Latitude); ErrorsContainer.SetErrors(() => Latitude, new[] { new ValidationResult(false, "Please use the N23°45.6 or S2345.6 format."), }); } RaiseErrorsChanged(() => Latitude); _saveCommand?.RaiseCanExecuteChanged(); OnPropertyChanged(() => LatitudeErrors); return(valid); }
/// <summary> /// プロパティの値を検証します。 /// </summary> /// <typeparam name="T">プロパティの型</typeparam> /// <param name="propertyName">プロパティ名</param> /// <param name="value">変更されたプロパティの値</param> /// <remarks> /// このメソッドは、属性による検証にのみ対応しています。 /// </remarks> private void ValidateProperty <T>(string propertyName, T value) { var context = new ValidationContext(this) { MemberName = propertyName }; var errors = new List <ValidationResult>(); var validate = Validator.TryValidateProperty(value, context, errors); if (validate) { // エラーなし _errorsContainer.ClearErrors(propertyName); } else { // エラーあり _errorsContainer.SetErrors(propertyName, errors.Select(x => x.ErrorMessage)); } }
public void WhenSettingNoErrorsForPropertyWithErrors_ThenNotifiesChangesAndHasNoErrors() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<ValidationResult>(pn => validatedProperties.Add(pn)); var validationError = new ValidationResult("message", new[] { "property1" }); validation.SetErrors("property1", new[] { validationError }); validatedProperties.Clear(); validation.SetErrors("property1", new ValidationResult[0]); Assert.IsFalse(validation.HasErrors); Assert.IsFalse(validation.GetErrors("property1").Any()); CollectionAssert.AreEqual(new[] { "property1" }, validatedProperties); }
public void WhenGettingErrorsWithPropertyName_ThenErrorsReturned() { List<string> validatedProperties = new List<string>(); var validation = new ErrorsContainer<string>(pn => validatedProperties.Add(pn)); validation.SetErrors("property1", new[] { "message" }); var errors = validation.GetErrors("property1"); Assert.True(errors.Any()); }