Skip to content

Commit

Permalink
Implement autofill
Browse files Browse the repository at this point in the history
  • Loading branch information
mr5z committed Nov 1, 2020
1 parent f0d95d4 commit c72ed51
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PropertyValidator.Test.Validation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions PropertyValidator/Models/RuleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ public RuleCollection<TModel> AddRule<TProperty>(
return this;
}

public RuleCollection<TModel> WithDelay(TimeSpan delay)
{
// TODO configure delay for next version. Issue #1
return this;
}

public List<IValidationRule> GetRules()
{
return validationRuleList;
Expand Down
4 changes: 4 additions & 0 deletions PropertyValidator/Models/ValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PropertyValidator.Models
{
Expand All @@ -13,6 +14,9 @@ public abstract class ValidationRule<T> : IValidationRule

public bool Validate(object value) => (HasError = !IsValid((T)value));

// TODO support async validations
//public virtual Task<bool> IsValidAsync(T value) => Task.FromResult(IsValid(value));

public abstract bool IsValid(T value);

public abstract string ErrorMessage { get; }
Expand Down
8 changes: 4 additions & 4 deletions PropertyValidator/PropertyValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<Authors>Mark Laureta</Authors>
<Company>Nkraft</Company>
<Description>A simple library to help you validate properties of class that implements INotifyPropertyChanged.</Description>
<PackageReleaseNotes>Partial fix for error aggregations and bug fixes</PackageReleaseNotes>
<PackageReleaseNotes>Implement autofill</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/mr5z/PropertyValidator</PackageProjectUrl>
<RepositoryUrl>https://github.com/mr5z/PropertyValidator</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<FileVersion>1.0.0.5</FileVersion>
<AssemblyVersion>1.0.0.5</AssemblyVersion>
<Version>1.0.4-patch</Version>
<FileVersion>1.0.0.6</FileVersion>
<AssemblyVersion>1.0.0.6</AssemblyVersion>
<Version>1.0.6</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion PropertyValidator/Services/IValidationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PropertyValidator.Services
public interface IValidationService
{
// For registration
RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel)
RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel, bool autofill = false)
where TNotifiableModel : INotifyPropertyChanged;

// Retrieve error messages per property
Expand Down
16 changes: 11 additions & 5 deletions PropertyValidator/Services/ValidationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public class ValidationService : IValidationService
{
private INotifyPropertyChanged notifiableModel;
private object ruleCollection;
private bool autofill;
private MethodInfo methodInfo;

public event EventHandler<ValidationResultArgs> PropertyInvalid;

public RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel)
public RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel, bool autofill)
where TNotifiableModel : INotifyPropertyChanged
{
this.notifiableModel = notifiableModel;
this.autofill = autofill;

ruleCollection = new RuleCollection<TNotifiableModel>(notifiableModel);
notifiableModel.PropertyChanged += NotifiableModel_PropertyChanged;
var type = typeof(RuleCollection<TNotifiableModel>);
Expand Down Expand Up @@ -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<string> GetErrorMessages<TNotifiableModel>(
Expand Down Expand Up @@ -93,7 +99,7 @@ private bool ValidateImpl(string propertyName = null)

public static bool ValidateRuleCollection(
List<IValidationRule> ruleCollection,
object owner,
object target,
string propertyName = null)
{
bool noErrors = true;
Expand All @@ -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;
}
Expand Down

0 comments on commit c72ed51

Please sign in to comment.