Validation Rules Plugin. There is an eBook call Enterprise Application Patterns using Xamarin.Forms. “The eBook focuses on core patterns and architectural guidance for developing Xamarin.Forms enterprise apps that are easier to test, maintain, and evolve. Guidance is provided on how to implement the Model-View-ViewModel (MVVM) pattern, dependency injection, navigation, validation, and configuration management, while maintaining loose coupling. In addition, there’s also guidance on performing authentication and authorization with IdentityServer, accessing remote data from containerized microservices, and unit testing.” Says David Britch – The author.
- Set up
- How to use it
Note: You can also see “Validating our Model” Documentation.
Any app that accepts input from users should ensure that the input is valid. Without validation, a user can supply data that causes the app to fail. Validation enforces business rules and prevents an attacker from injecting malicious data.
Improve the quality of your data using validation rules. Validation rules verify that the input an user enters in a record meets the standards you specify before the user can save the record. A validation rule can contain your own logic that evaluates the data in one or more fields and returns a value “True” or “False”. Validation rules also include an error message to display to the user when the rule returns a value “False” due to an invalid value.
You can specify the error message to display when an input fails validation and binding it to your UI to display it. Plugin.ValidationRules implement
INotifyPropertyChanged. So, you don’t need to worry about the update your UI.
Cool, now that we have an understanding of validation rules we can start!
Assuming that we already have our project created and we already know what the validation rules are, then we can proceed to install the Plugin.ValidationRules for our application in 3 simple steps:
- In Solution Explorer, right click on the project and select Manage NuGet Package….
- In the Search bar, type Plugin.ValidationRules or ValidationRules.
- Select and install the ValidationRules into your project.
First of all, you need to add the rules you want into your project. For that you must add a class that derives from
Once you have your rules, you need to create the properties you want to validate. Those properties must be a
ValidatableObject<T>. Your Properties don’t need to implement
INotifyPropertyChanged interface. Plugin.ValidationRules has his own implementation called
ExtendedPropertyChanged. So, the plugin do the job for you.
Before adding a validation rule to a property, be sure that you initialize the property. After that, add as many rules as you want to a property.
To validate a property, just call the myProperty.Validate() method. You can do it manually and use it in you ViewModel or code behind.
EXTRA: You can also add an EventToCommandBehavior and bind it to a command that call the validate method.
If you don’t want to validate each property one by one; create a method for validating all properties at the same time for you.
Or you can create a
ValidationUnit property and passing all property, which you want to validate at the same time, by the constructor. After that, you can call yourUnit.Validate() method to validate yours properties.
If there is an error after the validation you can get it calling the list errors _myProperty.Errors in your code or calling the property _myProperty.Error to get the first or default error of the list.
To bind your properties and errors to your XAML file; do it in the following way.
Plugin.ValidationRules is the easy way to add client-side validation of view model properties and notifies the user of any validation errors by showing them the control that contains the invalid data.
- Available on NuGet: Plugin.ValidationRules
- Repository: Validation Rules Plugin for Xamarin and Windows
- Examples: Plugin.ValidationRules
- You can also see: “Validating our Model” Documentation.
- Extension for Visual Studio: Validation Rule Template
- How to use Validation Rule Template
Microsoft Learn, the new Microsoft learning platfo...
I invite you to see my opinion on this topic and t...
I invite you to see my opinion on whether “I...
Opportunity to learn to develop mobile application...
As 2018 comes to an end, we are ready for this 201...
Shell, Visual y CollectionView are the new feature...
I’m Luis Matos from the Dominican Republic. I have +5 years working with .Net and +3 with Xamarin. Currently, I’m a Xamarin Certified Developer.
I invite you to leave me your feedback about this post in the comment box. If you want me to talk about a specific topic or detail a few more topics, let me know. The most interesting topics will be added to my list of future posts.