Xamarin MVC Project Separation & Fluent Validation – Part II

Continuing from Part I:

Fluent Validation 

So now we’ve got a project setup that shared our models and services, it’d be great to same the same validation for inputs or fields with minimum effort across multi-platforms.  Meaning if a “FirstName” is required, it would be great to not code that three separate times (Web, Android, and iOS).  With Fluent Validation we can achieve this with a little bit of setup on the mobile side.  On an .NET MVC site integrating FluentValidation is as easy as installing the correct NuGet package for your version of MVC.  It will integrate with un-obtrusive jQuery Validation automatically.  With Xamarin.Forms currently there is no solution to integrate so nicely.  However, I’ve created a framework that allows you to do something similar and have it work across the three platforms.

So one of the things you need in order to show validation is a Label, which will serve as our “Html.ValidationMessageFor” control in the web world and that label needs to be associated with some type of input.  Within the .NET MVC Platform this is achieved by a naming convention, however, we can’t necessarily achieve the same thing in Xamarin.Forms when we create our views via code, not XAML; but we can create our own naming convention!

Before we dive into the code, you should know that under normal circumstances you don’t new up a “Validator” (term for class that extends AbstractValidator) but you can! When you do there is a method you’d call called “Validate()”.  This method returns a “ValidationResult” with two properties: “IsValid” and “Errors” and “Errors” is a collection of “ValidationFailure”, which contains the Name and ErrorMessage for a particular property.  Knowing that we can create our own “unobtrusive” validation setup.  Lets begin!

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part II”