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 ValidateObject() { ErrorsContainer.ClearErrors(""); var validationResults = new List <ValidationResult>(); Validator.TryValidateObject(this, new ValidationContext(this, null, null), validationResults); ErrorsContainer.SetErrors("", validationResults); }
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()); }
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 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()); }
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 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 InsertSingleAndClear(string property) { var called = 0; var container = new ErrorsContainer <int>(_ => called++); container.SetErrors(property, new[] { 1 }); container.ClearErrors(property); called .Should().Be(2); container.HasErrors .Should().BeFalse(); container.GetErrors(property) .Should().BeEmpty(); }
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); } }
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)); } }
private bool ValidateIdentifier(string val) { var valid = (!string.IsNullOrWhiteSpace(val)) && (val.Length > 1) && (val.Length < 6); if (valid) { ErrorsContainer.ClearErrors(() => Identifier); } else { ErrorsContainer.ClearErrors(() => Identifier); ErrorsContainer.SetErrors(() => Identifier, new [] { new ValidationResult(false, "Identifier is mandatory. Should contain 2-5 letters and numbers.") }); } RaiseErrorsChanged(() => Identifier); _saveCommand?.RaiseCanExecuteChanged(); OnPropertyChanged(() => IdentifierErrors); return(valid); }
internal void ClearMockPropertyErrors() { _errorsContainer.ClearErrors <int>(() => MockProperty); }