Skip to content
Permalink
Browse files

Added ReactiveUI.Validation section in README. (#1968)

* Added ReactiveUI.Validation section.

* Updated Validations section.

* Updated git.
  • Loading branch information...
alexmartinezm authored and glennawatson committed Mar 13, 2019
1 parent fce49c8 commit 1b57e64d094e977be040cc64c7ded8974aea9c9b
Showing with 92 additions and 0 deletions.
  1. +92 −0 README.md
@@ -59,6 +59,7 @@ Install the following packages to start building your own ReactiveUI app. <b>Not
| Xamarin.Mac | [ReactiveUI][MacDoc] | [![CoreBadge]][Core] | [ReactiveUI.Events][CoreEvents] |
| Tizen | [ReactiveUI][CoreDoc] | [![CoreBadge]][Core] | [ReactiveUI.Events][CoreEvents] |
| Avalonia | [Avalonia.ReactiveUI][AvaDoc] | [![AvaBadge]][Ava] | None |
| Any | [ReactiveUI.Validation][ValidationsDocs] | [![ValidationsBadge]][ValidationsCore] | None

[Core]: https://www.nuget.org/packages/ReactiveUI/
[CoreEvents]: https://www.nuget.org/packages/ReactiveUI.Events/
@@ -103,6 +104,10 @@ Install the following packages to start building your own ReactiveUI app. <b>Not
[AvaDoc]: https://reactiveui.net/docs/getting-started/installation/avalonia
[EventsDocs]: https://reactiveui.net/docs/handbook/events/

[ValidationsCore]: https://www.nuget.org/packages/ReactiveUI.Validation/
[ValidationsBadge]: https://img.shields.io/nuget/v/ReactiveUI.Validation.svg
[ValidationsDocs]: https://reactiveui.net/docs/handbook/user-input-validation/

<h2>A Compelling Example</h2>

Let’s say you have a text field, and whenever the user types something into it, you want to make a network request which searches for that query.
@@ -202,6 +207,93 @@ public class ReactiveViewModel : ReactiveObject
}
```

<h2>Reactive Validations</h2>
ReactiveUI.Validations provides a subset of functions to create reactive validations, functioning in a reactive way.

<h3>How to use</h3>

* For those ViewModels which need validation, implement `ISupportsValidation`
* Add validation rules to the ViewModel
* Bind to the validation rules in the View

<h3>Example</h3>

Decorate existing ViewModel with `ISupportsValidation`, which has a single member, `ValidationContext`. The ValidationContext contains all of the functionality surrounding the validation of the ViewModel. Most access to the specification of validation rules is performed through extension methods on the ISupportsValidation interface. Then, add validation to the ViewModel.

```csharp
public class SampleViewModel : ReactiveObject, ISupportsValidation
{
public ValidationContext ValidationContext => new ValidationContext();
private string _name;
public string Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
public SampleViewModel()
{
// Name must be at least 3 chars. The selector is the property
// name and the line below is a single property validator.
this.ValidationRule(
vm => vm.Name,
name => !string.IsNullOrWhiteSpace(name),
"You must specify a name");
}
}
```

You can also use `ValidationHelper` to create reusable rules:
```csharp
public class SampleViewModel : ReactiveObject, ISupportsValidation
{
[...]
private int _age;
public int Age
{
get => _age;
set => this.RaiseAndSetIfChanged(ref _age, value);
}
public ValidationHelper AgeRule { get; set; }
public SampleViewModel()
{
// Age must be between 13 and 100, message includes the silly
// length being passed in, stored in a property of the ViewModel.
AgeRule = this.ValidationRule(
vm => vm.Age,
age => age >= 13 && age <= 100,
age => $"{age} is a silly age");
[...]
}
}
```

Finally, you bind to these validations from the View controls like below:

```csharp
public class SampleView : IViewFor<SampleViewModel>
{
public SampleView()
{
// Bind any validations which reference the name property to the text of the nameValidation control
this.BindValidation(ViewModel, vm => vm.Name, view => view.NameLabel.Text);
// Bind the validation specified by the AgeRule to the text of the ageValidation control
this.BindValidation(ViewModel, vm => vm.AgeRule, view => view.AgeLabel.Text);
}
}
```

<h3>NuGet Packages</h3>
Install the following package into you class library and platform-specific project. ReactiveUI.Validation package supports all platforms, including .NET Framework, .NET Standard, MonoAndroid, Tizen, UAP, Xamarin.iOS, Xamarin.Mac, Xamarin.TVOS.

This package was created based on [jcmm33's Vistian.Reactive.Validation](https://github.com/jcmm33/ReactiveUI.Validation) and maintained by [alexmartinezm](https://github.com/alexmartinezm).

<h2>Support</h2>

If you have a question, please see if any discussions in our [GitHub issues](https://github.com/reactiveui/ReactiveUI/issues) or [Stack Overflow](https://stackoverflow.com/questions/tagged/reactiveui) have already answered it.

0 comments on commit 1b57e64

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.