Giter Club home page Giter Club logo

xamarinforms-unobtrusivevalidationplugin's Introduction

Xamarin.Forms Unobtrusive FluentValidation Plugin

A plugin library that integrates the functionality of FluentValidation and Xamarin.Forms to provide an unobtrusive user experience.

The library follows an MVVM pattern and below are examples on how to get started.

Preview

alt text

Get Started

FluentValidation AbstractValidator (EnhancedAbstractValidator)

When using FluentValidation library, normally you would derive from AbstractValidator, however in this library you need to use EnhancedAbstractValidator.
This class extends AbstractValidator on and provides a new method RuleForProp. This method is essential in order for your view model later to work with the validator.

public class PersonValidator : EnhancedAbstractValidator<PersonViewModel>
{
   public PersonValidator()
   {
   	RuleForProp(a => a.FirstName)
   		.NotNull();
   } 
}

ViewModel (AbstractValidationViewModel)

When deriving from AbstractValidationViewModel, you will want to the class ValidatableProperty<>. This property encapsulates the needed propertys for the UI to bind to accordingly.

[FluentValidation.Attributes.Validator(typeof(PersonValidator))]
public class PersonViewModel : AbstractValidationViewModel
{
   public ValidatableProperty<string> FirstName { get; set; } = new ValidatableProperty<string>();
}

Xamarin.Forms UI Page

Code Page

   public class PersonPage : ContentPage
   {
       public PersonPage()
       {
           BindingContext = new PersonViewModel();

   		//Here we pass in the name of the property we would like this control to bind to. 
           var firstNameControl = new ValidatableEntryControl(nameof(PersonViewModel.FirstName));

           var sl = new StackLayout()
           {
               Orientation = StackOrientation.Vertical
           };

           sl.Children.Add(firstNameControl);
   		
           var submitButton = new Button()
   		{
   			Text = "Submit"
   		};

   		//Sample to trigger validator, normally you would call the validator from within your view model.
           submitButton.SetBinding(Button.CommandProperty, nameof(PersonViewModel.ValidateCommand));

           sl.Children.Add(submitButton);

           this.Content = sl;
       }

   }

XAML Page

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
   x:Class="Test.XamlTest"
   xmlns:custom="clr-namespace:Xamarin.Plugins.UnobtrusiveFluentValidation;assembly=Xamarin.Plugins.FluentValidation">
   <ContentPage.Content>
       <StackLayout Orientation="Vertical">
           <Label>Name</Label>
           <custom:ValidatableEntryControl BindingName="Name"></custom:ValidatableEntryControl>
           <Button x:Name="btnSubmit" Text="Submit" Command="{Binding ValidateCommand}"></Button>
       </StackLayout>
   </ContentPage.Content>
</ContentPage>	

xamarinforms-unobtrusivevalidationplugin's People

Contributors

james-russo avatar jrummell-elite avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.