Skip to content

winstongubantes/MatchaValidation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Matcha Validation Plugin an Unobtrusive Validation for Xamarin.Forms

A plugin library for unobtrusive "Testable" validation that works well with any MVVM framework , You can create a Customized Rule which perfectly fit to your needs.

Nuget

Matcha.Validation NuGet

Get Started

Using ValidationService

With ValidationService you can easily validate the viewmodel that derives from any MVVM framework base viewmodel class, this makes it very powerful and testable at the same time.

public class PrismTabbedPageViewModel : BindableBase
{
   private ValidationService<PrismTabbedPageViewModel> _validationService;

   public PrismTabbedPageViewModel()
   {
   	ValidationService = new ValidationService<PrismTabbedPageViewModel>(this);
   }

   public ValidationService<PrismTabbedPageViewModel> ValidationService
   {
   	get => _validationService;
   	set => SetProperty(ref _validationService, value);
   }
}

ValidationRule

When deriving ValidationRule, this will enables validation of the viewmodels property, The validation could be anything from email, null, greaterthanzero or it could be your custom validation (take EmailRule as an example).

public class IsNotNullOrEmptyRule : ValidationRule<string>
{
   public IsNotNullOrEmptyRule(string propertyName) : base(propertyName)
   {
   	ValidationMessage = $"{propertyName} should not be empty!";
   }

   protected override bool CheckValue(string value)
   {
   	return IsValid = !string.IsNullOrWhiteSpace(value);
   }
}

Adding the ValidationRule to ValidationService

ValidationRule is a bindable class this what makes the Matcha Validation so powerful because of its pure unobtrusive and it doesnt require custom control to handle it.

public PrismTabbedPageViewModel()
{
   ValidationService = new ValidationService<PrismTabbedPageViewModel>(this);
   EntryNotEmptyRule = ValidationService.Add(e => new IsNotNullOrEmptyRule(nameof(e.UserName))); //
}

Check if all Rule is Valid

ValidationRule is a bindable class this what makes the Matcha Validation so powerful because of its pure unobtrusive and it doesnt require custom control to handle it.

if (ValidationService.IsValid)
{
   //You code here when all rule's are valid
}

XAML Page

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
   x:Class="Sample.Views.PrismTabbedPage"
   xmlns="http://xamarin.com/schemas/2014/forms"
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   xmlns:converter="clr-namespace:Sample.Converter;assembly=Sample"
   xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
   prism:ViewModelLocator.AutowireViewModel="True">
   <TabbedPage.Resources>
       <ResourceDictionary>
           <converter:InverseBooleanConverter x:Key="BooleanConverter" />
       </ResourceDictionary>
   </TabbedPage.Resources>
   <ContentPage Title="Custom Validation">
       <StackLayout Padding="30">
           <Entry Placeholder="Username" Text="{Binding UserName}" />
           <Label
               FontSize="Micro"
               IsVisible="{Binding EntryNotEmptyRule.IsValid, Converter={StaticResource BooleanConverter}}"
               Text="{Binding EntryNotEmptyRule.ValidationMessage}"
               TextColor="Red" />
           <Button
               Command="{Binding ValidateBuilInCommand}"
               IsEnabled="{Binding ValidationService.IsValid}"
               Text="Validate" />
       </StackLayout>
   </ContentPage>
</TabbedPage>

Preview

alt text

Platform Supported

Platform Version
Xamarin.iOS iOS 7+
Xamarin.Android API 15+
Windows 10 UWP 10+
Xamarin.Mac All
Xamarin.tvOS All
Xamarin.watchOS All
.NET Standard 2.0+
.NET Core 2.0+

About

A plugin library for unobtrusive (pure) validation that works well with any MVVM framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages