Validation Snippets in Xamarin Forms

— This article is part of the snippet June calendar by Luis Matos, click here to check all the great contributions”.

Some time ago I read this great article in the Xamarin blog about adding validations in Xamarin Forms Enterprise Apps. In that post, they talked about a great validation model based on Validable objects, where if you want to add a new validation rule you just have to create a new ValidationRule class.

In this article, I’ll extend that post by adding different validation options to use with this validation model.

How it works

Basically we have an IValidatable interface, which will represent our validation objects, expose the validation rules, and provide a validation method.

To create a new rule, we create a class that implements the IValidationRule interface, and we will do our custom validation logic inside the check method.

Validations Snippets

Here is a list of common validation use cases:

IsNotNullOrEmptyRule: Validates if the value is not empty

IsLenghtValidRule: Validates if the length is valid. You have to specify a Minimum/Maximum length.

HasValidAgeRule: Applicable to date values (For example in a DatePicker). Check if the user has at least 18 years old.

IsValueTrueRule: Applicable for boolean values (For example in a CheckBox). Check if the value is true

IsValidEmailRule: Check if the email is valid.

IsValidPasswordRule: Check if the password matches the regex specified, by default it has a regex that demands a password between 8-20 digits, one upper case character, one lower case character, one number, and one special character.

MatchPairValidationRule: Check if two elements match. For example validate Email/Confirm Email, Password/Confirm Password.

For this Validation, I extended the ValidableObject class to have two bindable objects. Copy the code here.

Let’s test

To test these validation rules, I created a signup page that validates:

  • First Name -> The entry is not empty
  • Last Name -> The entry is not empty
  • Birthday -> The DatePicker’s date must be a minimum of 18 years apart from the current date
  • Phone Number -> Should have a maximum of 10 digits and a minimum of 8
  • Email/Confirm email -> The entry should have a valid email and both should match
  • Password/Confirm password -> The entry must contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character. Also, both should match
  • Terms And Condition -> The terms and condition checkbox should be checked.

So the ViewModel will look like this:

— We create our ValidableObjects, if I want to validate two elements that are related to the same value you have to use ValidatablePair.

— We add the rules to these objects and we set the message we want the UI to display when the validation fails.

The ValidableObject has a Validate() method that we call to perform the validation.

Check the full SignUpViewModel here.

Finally, in the view I just do the binding to these elements, using the properties Value, HasErrors , Errors and IsValid.

Since the errors are a list, I use this converter to get just the first error, and this converter to check if IsValid property is false. Also, I have this behavior to change the line to red if there is an error.

That’s all, you can copy the full source code here.

Happy coding! 🙂

You may also like

4 Comments

  1. Helpful article Charlin. thanks.
    here’s I saw this type of errors validation in xamarin.forms was same as what we do in WPF and even older on Silverlight.

  2. It was really great article.
    Just wanted to add, if you can mention the use of Fody as well, I struggle a lot as it was not validating the property.