Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mr5z committed Dec 18, 2023
2 parents 88d97a4 + f69bb44 commit 4dbcb4c
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ Install-Package PropertyValidator
```

## Example Usage
Here's an example of how to use PropertyValidator in your code:

### Prepare INPC class

```csharp
using PropertyValidator;

// ...
class ViewModel : INotifyPropertyChanged, INotifiableModel
{
// We are just converting the function to readonly property. This will be accessed by XAML later.
public IDictionary<string, string?> Errors => validationService.GetErrors();

// Implement INotifiableModel so it propagates changes to XAML
public void NotifyErrorPropertyChanged() => RaisePropertyChanged(nameof(Errors));

// I have only included this here for clarity. Substitute with your own implentation.
private void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```

### Setup INPC class in its initialization code
```csharp
var validationService = new ValidationService();

// Register your model for validation
Expand All @@ -30,9 +47,24 @@ validationService.For(this, delay: TimeSpan.FromSeconds(0.7))
.AddRule(e => e.EmailAddress, new RequiredRule(), new LengthRule(100), new EmailFormatRule());
```

### Xamarin Forms code example
1. [ViewModel](https://github.com/mr5z/Test.Maui/blob/main/Test.Maui/ViewModels/MainPageViewModel.cs)
2. [XAML](https://github.com/mr5z/Test.Maui/blob/main/Test.Maui/Pages/MainPage.xaml)
### Consume in XAML
```xaml
<Entry
Text="{Binding EmailAddress}"
Keyboard="Email"
Placeholder="Email address" />
<Label
Text="{Binding Errors[EmailAddress]}"
TextColor="Red"
FontSize="Small" />
...
```

### Result
![output](https://github.com/mr5z/PropertyValidator/assets/6318395/410f7c92-e76e-4a80-b309-d0dd0bc1afbd)

### MAUI complete example
[Test.Maui](https://github.com/mr5z/Test.Maui)

## Features
PropertyValidator offers a range of features to streamline your validation process:
Expand Down

0 comments on commit 4dbcb4c

Please sign in to comment.