Skip to content
Validation helpers for ReactiveUI based solutions, functioning in reactive way.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Housekeeping: add open collective bot configuration file Apr 1, 2019
media Added logo. Feb 24, 2019
samples/xamarin-forms/LoginApp Added an analyzers, support for multiple validation and added Xamarin… Mar 19, 2019
src Fix #16 and added test. (#17) Apr 1, 2019
.gitattributes Added an analyzers, support for multiple validation and added Xamarin… Mar 19, 2019
.gitignore housekeeping: Add android support/CI engine support Feb 26, 2019
LICENSE Added an analyzers, support for multiple validation and added Xamarin… Mar 19, 2019
README.md Housekeeping: Update README.md to contain updated Android (#15) Mar 24, 2019
analyzers.ruleset
analyzers.tests.ruleset Added an analyzers, support for multiple validation and added Xamarin… Mar 19, 2019
azure-pipelines.yml housekeeping: Add android support/CI engine support Feb 26, 2019
build.cake housekeeping: add unit tests Feb 26, 2019
build.cmd
build.config
build.ps1 housekeeping: Add android support/CI engine support Feb 26, 2019
build.sh housekeeping: Add android support/CI engine support Feb 26, 2019
directory.build.props
stylecop.json Added an analyzers, support for multiple validation and added Xamarin… Mar 19, 2019
version.json housekeeping: Add android support/CI engine support Feb 26, 2019

README.md

NuGet Stats Build Status Code Coverage

ReactiveUI.Validation

Validation for ReactiveUI based solutions, functioning in a reactive way. This repository is based on jcmm33's Vistian.Reactive.Validation.

NuGet Packages

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.

Platform ReactiveUI Package NuGet
Any ReactiveUI.Validation CoreBadge

How to use

  • For ViewModels which need validation, implement ISupportsValidation.
  • Add validation rules to the ViewModel.
  • Bind to the validation rules in the View.

Example

  1. 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.
public class SampleViewModel : ReactiveObject, ISupportsValidation
{
    public SampleViewModel()
    {
        // Creates the validation for the Name property
        this.ValidationRule(
            viewModel => viewModel.Name,
            name => !string.IsNullOrWhiteSpace(name),
            "You must specify a valid name");
    }

    public ValidationContext ValidationContext => new ValidationContext();

    private string _name;
    public string Name
    {
        get => _name;
        set => this.RaiseAndSetIfChanged(ref _name, value);
    }
}
  1. Add validation presentation to the View.
public class SampleView : ReactiveContentPage<SampleViewModel>
{
    public SampleView()
    {
        InitializeComponent();

        this.WhenActivated((CompositeDisposable disposables) =>
        {
            this.Bind(ViewModel, vm => vm.Name, view => view.Name.Text)
                .DisposeWith(disposables);

            // Bind any validations which reference the Name property 
            // to the text of the NameError UI control
            this.BindValidation(ViewModel, vm => vm.Name, view => view.NameError.Text)
                .DisposeWith(disposables);
        });
    }
}

Note: Name is an Entry and NameError is a Label (both are controls from the Xamarin.Forms library).

Example with Android extensions

There are extensions methods for Android specific and its Material design control TextInputLayout. These extensions use internally the Error property from this control, allowing you a Material Design and fully native behavior to showing errors.

To use these extensions you must import ReactiveUI.Validation.Platforms.Android.

using ReactiveUI.Validation.Platforms.Android;

namespace SampleApp.Activities
{
    public class SampleActivity : ReactiveAppCompatActivity<SampleViewModel>
    {
        public TextInputEditText Name { get; set; }

        public TextInputLayout NameLayout { get; set; }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Sets our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            WireUpControls();

            this.Bind(ViewModel, vm => vm.Name, view => view.Name.Text);

            // Bind any validations which reference the Name property 
            // to the Error property of the TextInputLayout control
            this.BindValidation(ViewModel, vm => vm.Name, NameLayout);
        }
    }
}

Capabilities

  1. Rules can be composed of single or multiple properties along with more generic Observables.
  2. Validation text can encapsulate both valid and invalid states.
  3. Binding can occur to either a View or an action.
  4. Validation text can reference either the ViewModel or properties which comprise the validation rule e.g. include text entered as part of validation message.
  5. Validation text output can be adjusted using custom formatters, not only allowing for single & multiline output but also for platforms like Android it should be possible to achieve richer renderings i.e. Bold/italics.

How it Works

In essence, it's a relatively simple model of the ValidationContext containing a list of IValidationComponent instances. An IValidationComponent provides an observable for ValidationState. Whenever validation state changes (either a transition of validity) or ValidationText changes, then a new value is pushed out.

Feedback

Please use GitHub issues for questions or comments.

Authors

Copyright and license

Code released under the MIT license.

You can’t perform that action at this time.