From c72ed51dd481a72818aa1f047e5bd6e51e32721a Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 1 Nov 2020 22:08:08 +0800 Subject: [PATCH] Implement autofill --- .../Services/ToastService.cs | 2 +- .../Validation/Address/CountryIsoCodeRule.cs | 1 + .../ViewModels/ItemsPageViewModel.cs | 4 ++-- PropertyValidator/Models/RuleCollection.cs | 6 ------ PropertyValidator/Models/ValidationRule.cs | 4 ++++ PropertyValidator/PropertyValidator.csproj | 8 ++++---- PropertyValidator/Services/IValidationService.cs | 2 +- PropertyValidator/Services/ValidationService.cs | 16 +++++++++++----- 8 files changed, 24 insertions(+), 19 deletions(-) diff --git a/PropertyValidator.Test/PropertyValidator.Test.Android/Services/ToastService.cs b/PropertyValidator.Test/PropertyValidator.Test.Android/Services/ToastService.cs index 21e6f50..57e62ac 100644 --- a/PropertyValidator.Test/PropertyValidator.Test.Android/Services/ToastService.cs +++ b/PropertyValidator.Test/PropertyValidator.Test.Android/Services/ToastService.cs @@ -26,7 +26,7 @@ public ToastService(Context context) public void ShowMessage(string message, params string[] args) { toast?.Cancel(); - toast = Toast.MakeText(context, string.Format(message, args), ToastLength.Short); + toast = Toast.MakeText(context, string.Format(message, args), ToastLength.Long); toast.Show(); } } diff --git a/PropertyValidator.Test/PropertyValidator.Test/Validation/Address/CountryIsoCodeRule.cs b/PropertyValidator.Test/PropertyValidator.Test/Validation/Address/CountryIsoCodeRule.cs index 79a9906..552ac49 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/Validation/Address/CountryIsoCodeRule.cs +++ b/PropertyValidator.Test/PropertyValidator.Test/Validation/Address/CountryIsoCodeRule.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace PropertyValidator.Test.Validation { diff --git a/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs b/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs index dd74138..55e211e 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs +++ b/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs @@ -42,13 +42,13 @@ public ItemsPageViewModel( public void Initialize(INavigationParameters parameters) { - validationService.For(this) + validationService.For(this, autofill: true) .AddRule(e => e.FirstName, new RequiredRule(), new MinLengthRule(2)) .AddRule(e => e.LastName, new MaxLengthRule(5)) .AddRule(e => e.EmailAddress, new EmailFormatRule()) .AddRule(e => e.PhysicalAddress, "Deez nuts!", new AddressRule()); - validationService.PropertyInvalid += ValidationService_PropertyInvalid; + //validationService.PropertyInvalid += ValidationService_PropertyInvalid; } private void ValidationService_PropertyInvalid(object sender, ValidationResultArgs e) diff --git a/PropertyValidator/Models/RuleCollection.cs b/PropertyValidator/Models/RuleCollection.cs index 4d049f1..5cada36 100644 --- a/PropertyValidator/Models/RuleCollection.cs +++ b/PropertyValidator/Models/RuleCollection.cs @@ -74,12 +74,6 @@ public RuleCollection AddRule( return this; } - public RuleCollection WithDelay(TimeSpan delay) - { - // TODO configure delay for next version. Issue #1 - return this; - } - public List GetRules() { return validationRuleList; diff --git a/PropertyValidator/Models/ValidationRule.cs b/PropertyValidator/Models/ValidationRule.cs index 5b910c4..d61eee4 100644 --- a/PropertyValidator/Models/ValidationRule.cs +++ b/PropertyValidator/Models/ValidationRule.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace PropertyValidator.Models { @@ -13,6 +14,9 @@ public abstract class ValidationRule : IValidationRule public bool Validate(object value) => (HasError = !IsValid((T)value)); + // TODO support async validations + //public virtual Task IsValidAsync(T value) => Task.FromResult(IsValid(value)); + public abstract bool IsValid(T value); public abstract string ErrorMessage { get; } diff --git a/PropertyValidator/PropertyValidator.csproj b/PropertyValidator/PropertyValidator.csproj index 948892a..ba63805 100644 --- a/PropertyValidator/PropertyValidator.csproj +++ b/PropertyValidator/PropertyValidator.csproj @@ -6,13 +6,13 @@ Mark Laureta Nkraft A simple library to help you validate properties of class that implements INotifyPropertyChanged. - Partial fix for error aggregations and bug fixes + Implement autofill https://github.com/mr5z/PropertyValidator https://github.com/mr5z/PropertyValidator LICENSE - 1.0.0.5 - 1.0.0.5 - 1.0.4-patch + 1.0.0.6 + 1.0.0.6 + 1.0.6 en diff --git a/PropertyValidator/Services/IValidationService.cs b/PropertyValidator/Services/IValidationService.cs index 1c150b7..66a3ade 100644 --- a/PropertyValidator/Services/IValidationService.cs +++ b/PropertyValidator/Services/IValidationService.cs @@ -8,7 +8,7 @@ namespace PropertyValidator.Services public interface IValidationService { // For registration - RuleCollection For(TNotifiableModel notifiableModel) + RuleCollection For(TNotifiableModel notifiableModel, bool autofill = false) where TNotifiableModel : INotifyPropertyChanged; // Retrieve error messages per property diff --git a/PropertyValidator/Services/ValidationService.cs b/PropertyValidator/Services/ValidationService.cs index 6dcb30e..de54683 100644 --- a/PropertyValidator/Services/ValidationService.cs +++ b/PropertyValidator/Services/ValidationService.cs @@ -13,14 +13,17 @@ public class ValidationService : IValidationService { private INotifyPropertyChanged notifiableModel; private object ruleCollection; + private bool autofill; private MethodInfo methodInfo; public event EventHandler PropertyInvalid; - public RuleCollection For(TNotifiableModel notifiableModel) + public RuleCollection For(TNotifiableModel notifiableModel, bool autofill) where TNotifiableModel : INotifyPropertyChanged { this.notifiableModel = notifiableModel; + this.autofill = autofill; + ruleCollection = new RuleCollection(notifiableModel); notifiableModel.PropertyChanged += NotifiableModel_PropertyChanged; var type = typeof(RuleCollection); @@ -48,7 +51,10 @@ private void NotifiableModel_PropertyChanged(object sender, PropertyChangedEvent .GroupBy(it => it.PropertyName) .ToDictionary(group => group.Key, g => g.Select(it => it.ErrorMessage)); - PropertyInvalid?.Invoke(this, new ValidationResultArgs(e.PropertyName, errorMessages)); + var eventArgs = new ValidationResultArgs(e.PropertyName, errorMessages); + if (autofill) + eventArgs.FillErrorProperty(notifiableModel); + PropertyInvalid?.Invoke(this, eventArgs); } public List GetErrorMessages( @@ -93,7 +99,7 @@ private bool ValidateImpl(string propertyName = null) public static bool ValidateRuleCollection( List ruleCollection, - object owner, + object target, string propertyName = null) { bool noErrors = true; @@ -102,8 +108,8 @@ public static bool ValidateRuleCollection( if (!string.IsNullOrEmpty(propertyName) && rule.PropertyName != propertyName) continue; - var property = owner.GetType().GetProperty(rule.PropertyName); - var value = property.GetValue(owner, null); + var property = target.GetType().GetProperty(rule.PropertyName); + var value = property.GetValue(target, null); rule.Validate(value); noErrors = noErrors && !rule.HasError; }