Forum Discussion

nogginbox's avatar
nogginbox
Copper Contributor
Nov 17, 2021

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

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 https://hub.packtpub.com/how-to-implement-data-validation-with-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?

No RepliesBe the first to reply

Resources