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.
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();
}
}
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>();
}
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;
}
}
<?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>