Realtime Validation in Xamarin Forms with FluentValidation
Xamarin Forms doesn’t have anything baked into the framework to handle data validation, but it’s actually possible to do in your cross platform code without any renderers.
Microsoft has had for a long time the interface INotifyDataErrorInfo. You’re going to want to implement this on your ViewModel (or better yet, your base view model) whereby you can notify the View whenever there are errors. On top of this, you want to also bring in Jeremy Skinner’s FluentValidation library to handle your validation rules.
Let’s start by wiring up our BaseViewModel. This will implement the INotifyDataErrorInfo as well as INotifyPropertyChanged. You’ll notice in here the SetProperty
method that simply helps us invoke validation in our subsequest ViewModel.
Next we want to set up all of our validation rules. Using FluentValidation is super cool because it allows us to separate out the logic cleanly and also makes validation fully testable. Here’s a super simple example of our validator.
From here, all of our ViewModels will be implemented as follows. note: ValidateProperty will get called every time the setter updates the value
Now that our ViewModel is wired up, we need to build out our View. The problem that exists with XamarinForms as it stands right now is that there is not out-of-the-box error messaging built into any of the controls. To enable this, we need to first build a custom control to handle it. Here’s a simple ExtendedEntry
that will get you started.
In here we’re leveraging a ControlTemplate
to watch our bindings and we have a special extension method to extract the binding Path
The last step is the implementation on the View.
And that’s it. If you run this in your simulator, you’ll be able to see real time error messages appearing benieth your Entry fields.