All Projects → pglazkov → Mvvmvalidation

pglazkov / Mvvmvalidation

Licence: mit
Lightweight library that helps reduce boilerplate when implementing validation in XAML MVVM applications

Projects that are alternatives of or similar to Mvvmvalidation

Reactiveproperty
ReactiveProperty provides MVVM and asynchronous support features under Reactive Extensions. Target framework is .NET Standard 2.0.
Stars: ✭ 603 (+327.66%)
Mutual labels:  xamarin, uwp, mvvm, wpf, xaml
Xamarin Demos
This repository contains the Syncfusion Xamarin UI control’s samples and the guide to use them.
Stars: ✭ 218 (+54.61%)
Mutual labels:  xamarin, uwp, mvvm, xaml
Xaml Code Experiences
A collection of the experiences I have collected during days of Xamarin and Wpf, while following the MVVM design pattern.
Stars: ✭ 114 (-19.15%)
Mutual labels:  xamarin, mvvm, wpf, xaml
Adaptivecards
A new way for developers to exchange card content in a common and consistent way.
Stars: ✭ 950 (+573.76%)
Mutual labels:  xamarin, uwp, wpf, xaml
Catel
An application development platform
Stars: ✭ 616 (+336.88%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Reactiveui
An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
Stars: ✭ 6,709 (+4658.16%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Uno
Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
Stars: ✭ 6,029 (+4175.89%)
Mutual labels:  xamarin, uwp, mvvm, xaml
Caliburn.micro
A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
Stars: ✭ 2,404 (+1604.96%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Mvvmcross
The .NET MVVM framework for cross-platform solutions, including Xamarin.iOS, Xamarin.Android, Windows and Mac.
Stars: ✭ 3,594 (+2448.94%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Ammyui
Ammy language repository
Stars: ✭ 356 (+152.48%)
Mutual labels:  xamarin, uwp, wpf, xaml
Mvvmlight
The main purpose of the toolkit is to accelerate the creation and development of MVVM applications in Xamarin.Android, Xamarin.iOS, Xamarin.Forms, Windows 10 UWP, Windows Presentation Foundation (WPF), Silverlight, Windows Phone.
Stars: ✭ 973 (+590.07%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Xamarin Forms Gtk Movies Sample
The Movie DB Xamarin.Forms Sample
Stars: ✭ 83 (-41.13%)
Mutual labels:  xamarin, uwp, wpf, xaml
Arcgis Toolkit Dotnet
Toolkit for ArcGIS Runtime SDK for .NET
Stars: ✭ 125 (-11.35%)
Mutual labels:  xamarin, uwp, wpf, xaml
Csla
A home for your business logic in any .NET application.
Stars: ✭ 865 (+513.48%)
Mutual labels:  netstandard, xamarin, uwp, wpf
Waf
Win Application Framework (WAF) is a lightweight Framework that helps you to create well structured XAML Applications.
Stars: ✭ 539 (+282.27%)
Mutual labels:  xamarin, uwp, mvvm, wpf
arcgis-runtime-demos-dotnet
Demo applications provided by the ArcGIS Runtime SDK for .NET Team
Stars: ✭ 51 (-63.83%)
Mutual labels:  xaml, xamarin, uwp, wpf
Arcgis Runtime Samples Dotnet
Sample code for ArcGIS Runtime SDK for .NET – UWP, WPF, Xamarin.Android, Xamarin.iOS, and Xamarin.Forms
Stars: ✭ 274 (+94.33%)
Mutual labels:  xamarin, uwp, wpf, xaml
Windowscommunitytoolkit
The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building UWP and .NET apps for Windows 10. The toolkit is part of the .NET Foundation.
Stars: ✭ 4,654 (+3200.71%)
Mutual labels:  uwp, mvvm, wpf, xaml
Reactivemvvm
Cross-platform ReactiveUI sample app built for a talk at MSK .NET conf.
Stars: ✭ 94 (-33.33%)
Mutual labels:  xamarin, uwp, mvvm, wpf
Rapid Xaml Toolkit
Tools to accelerate XAML development within Visual Studio.
Stars: ✭ 427 (+202.84%)
Mutual labels:  uwp, wpf, xaml

Build status

MVVM Validation Helpers

MVVM Validation Helpers is a lightweight library that makes it easier for developers to implement validation in MVVM applications.

It allows you to:

  • Define and keep all your validation rules conveniently in one place.
  • Reduce boilerplate of maintaining error list for each of the validation targets (properties).
  • Validate a target and get the validation result back without worrying what rules need to be checked.
  • Easily implement INotifyDataErrorInfo interface in your view models to integrate with XAML binding engine for displaying validation errors in your views (see NotifyDataErrorAdapter below).

Getting Started

Install the NuGet package.

OR

Download the binaries from Releases.

Examples

Creating an instance of the helper

In order to use the library you need to create an instance of the ValidationHelper helper class and store it in an instance field of the class where you want to implement validation. If you do a lot of validation, it is probably the best to do it in your ViewModelBase class.

public class ViewModelBase
{
    public ViewModelBase()
    {
        Validator = new ValidationHelper();
    }
    
    protected ValidationHelper Validator { get; private set; }
}

Adding a simple validation rule

Validator.AddRule(nameof(FirstName),
                  () => RuleResult.Assert(!string.IsNullOrEmpty(FirstName), "First Name is required"));

OR

Validator.AddRequiredRule(() => FirstName, "First Name is required");

Adding a rule that depends on two different properties

Such a rule will be executed whenever you validate either of those properties.

Validator.AddRule(nameof(RangeStart),
                  nameof(RangeEnd),
                  () => RuleResult.Assert(RangeEnd > RangeStart, 
                                          "RangeEnd must be grater than RangeStart");

Adding an asynchronous rule

Such rule can perform more complex validation that may take long time or cannot be executed synchronously, for example, a call to a web service.

Validator.AddAsyncRule(nameof(UserName),
    async () =>
    {
        var isAvailable = await UserRegistrationService.IsUserNameAvailable(UserName).ToTask();

        return RuleResult.Assert(isAvailable, 
            string.Format("User Name {0} is taken. Please choose a different one.", UserName));
    });

Executing validation

// Validate all (execute all validation rules)
ValidationResult validationResult = Validator.ValidateAll();

// Validate a specific target
ValidationResult validationResult = Validator.Validate(nameof(FirstName));

Executing validation asynchronously

// Validate all (execute all validation rules)
ValidationResult validationResult = await Validator.ValidateAllAsync();

// Validate a specific target
ValidationResult validationResult = await Validator.ValidateAsync(nameof(FirstName));

Getting current validation state at any point of time

Any time you can request current validation state for the entire object or for specific validation targets.

// Get validation result for the entire object
var validationResult = Validator.GetResult();

// Get validation result for a target
var firstNameValidationResult = Validator.GetResult(nameof(FirstName));

Receive notifications when validation result changes

Validator.ResultChanged += OnValidationResultChanged;

private void OnValidationResultChanged(object sender, ValidationResultChangedEventArgs e)
{
    // Get current state of the validation
    ValidationResult validationResult = Validator.GetResult();

    UpdateValidationSummary(validationResult);
}

Implement INotifyDataErrorInfo interface

The library includes NotifyDataErrorAdapter class that makes the implementation of INotifyDataErrorInfo interface in your view models trivial.

public class ValidatableViewModelBase : INotifyDataErrorInfo
{
    protected ValidationHelper Validator { get; private set; }
    private NotifyDataErrorInfoAdapter NotifyDataErrorInfoAdapter { get; set; }

    public ValidatableViewModelBase()
    {
        Validator = new ValidationHelper();

        NotifyDataErrorInfoAdapter = new NotifyDataErrorInfoAdapter(Validator);
    }

    public IEnumerable GetErrors(string propertyName)
    {
        return NotifyDataErrorInfoAdapter.GetErrors(propertyName);
    }

    public bool HasErrors
    {
        get { return NotifyDataErrorInfoAdapter.HasErrors; }
    }

    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged
    {
        add { NotifyDataErrorInfoAdapter.ErrorsChanged += value; }
        remove { NotifyDataErrorInfoAdapter.ErrorsChanged -= value; }
    }
}

For more examples download the source code and check out the example project.

Sample UI Screenshot

Advanced Use Cases

Execute rules even if the target is already invalid

By default, if the first rule for the target failed, the remaining rules for that target are skipped. For example, if the "Email" field is required it doesn't make sense to check that the email address is in valid format until user has entered something, so first we execute the "required" rule and only if it succeds we execute the next rule to verify the format of the email.

This default behavior can be overriden either globally or per rule:

Globally:

Validator = new ValidationHelper(new ValidationSettings
{
    DefaultRuleSettings = new ValidationRuleSettings
    {
        ExecuteOnAlreadyInvalidTarget = true
    }
});

Per Rule:

Validator.AddAsyncRule(/* Rule */)
         .WithSettings(s => s.ExecuteOnAlreadyInvalidTarget = false);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].