What's the 'best' way to validate user input in .NET MAUI?

Occasional Contributor

I'm trying to work out how to add data validation to a XAML page driven by a view model. With all the flavours of XAML there is a lot of how to guides, but I'm not sure if they all apply and some of them seem less simple than I'd like.


I found this talking about WPF:

There are four ways to use the Validation that comes with WPF:
- Validation Rules (property of the Binding object)
- Throw an Exception in the property setter and set ValidatesOnException to true on your Binding
- Implement IDataErrorInfo and set ValidatesOnDataErrors to true on your Binding
- Implement INotifyDataErrorInfo

INotifyDataErrorInfo is the one that I would recommend today. It's available since .NET 4.5.


Is INotifyDataError the best way to go. I found this guide on how to implement INotifyDataError in Xamarin Forms  but it's quite long and I was really hoping for something simpler. I'm kind of used to .NET MVC and popping an attribute on my view that automatically kicks in as part of model binding.


So, what is the best way to go, or what are the different options and what are the pros and cons of each?

0 Replies