Xamarin.AttributeValidation 2.0.1

dotnet add package Xamarin.AttributeValidation --version 2.0.1
NuGet\Install-Package Xamarin.AttributeValidation -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xamarin.AttributeValidation" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xamarin.AttributeValidation --version 2.0.1
#r "nuget: Xamarin.AttributeValidation, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xamarin.AttributeValidation as a Cake Addin
#addin nuget:?package=Xamarin.AttributeValidation&version=2.0.1

// Install Xamarin.AttributeValidation as a Cake Tool
#tool nuget:?package=Xamarin.AttributeValidation&version=2.0.1

Get it on NuGet: https://www.nuget.org/packages/Xamarin.AttributeValidation/

Nuget

Xamarin.AttributeValidation

This library allows to use attributes in the ViewModel to validate input fields. The usage is very similar to ASP.NET Core Form validation.

Take a look at the examples in this repository or:

Step by Step:

  1. Add a reference to Xamarin.AttributeValidation via NuGet.
  2. Assuming your XAML looks something like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" x:Class="Example.Views.RegisterPage">
    <ContentPage.Content>
        <StackLayout HorizontalOptions="FillAndExpand">
            <Entry x:Name="Input_Name" Text="{Binding Name}" Keyboard="Default" Placeholder="Name" Margin="0,0,0,15" />
            <Entry x:Name="Input_Email" Text="{Binding Email}" Keyboard="Email" Placeholder="Email" Margin="0,0,0,15" />
            <Entry x:Name="Input_Password" Text="{Binding Password}" IsPassword="True" Placeholder="Password" Margin="0,0,0,15" />
            <Button x:Name="Button_Register" Text="Create Account" Clicked="Register_Clicked" Margin="0,0,0,15" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
  1. The ViewModel looks like this:
using Example.Localization;
using Xamarin.AttributeValidation.Attributes;

namespace Example.Mobile.ViewModels
{
    public class RegisterViewModel : INotifyPropertyChanged
    {
        //The EmailAddress attribute makes sure that the value is a valid Email Address.
        [EmailAddress(typeof(StringResources), nameof(StringResources.Error_invalidEmailAddress))]
        public string Email { get; set; }

        //The Password Attribute makes sure the value is a strong enough password. You can customize this via properties. (See below)
        [Password(typeof(StringResources), nameof(StringResources.Error_weakPassword))]
        public string Password { get; set; }

        //The StringLength Attribute makes sure the value is of the specified length.
        [StringLength(2, typeof(StringResources), nameof(StringResources.Error_invalidName))]
        public string Name { get; set; }
    }
}
  1. The corresponding CS file looks like this:
using ...
using Xamarin.AttributeValidation;

namespace Example.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RegisterPage : ContentPage
    {
        private RegisterViewModel viewModel;

        public RegisterPage()
        {
            InitializeComponent();

            BindingContext = viewModel = new RegisterViewModel();
        }

        private async void Register_Clicked(object sender, EventArgs e)
        {
            //Pass the viewModel as parameter
            var isValid = await this.ValidateAsync();
            if (isValid)
                await DisplayAlert("Validation", "Model is valid", "OK");
            //isValid holds a boolean, representing if the ViewModel is valid or not.
            //The Validator will automatically display the errormessages in the UI.
        }
    }
}

Supported Attributes:

Every Attribute must be given an error message in its constructor. Since Attributes are hardcoded into the assembly, you can only pass hardcoded strings. But don't worry! Every Attribute has an overload, where you can pass the type and the key of a resource file.

IMPORTANT! The Resource File must have code generation enabled and set to "external tool" (public properties, NOT internal!). I highly recommend the ResX Manager: https://marketplace.visualstudio.com/items?itemName=TomEnglert.ResXManager

Password

//Using the overload to use the localized string value.
[Password(typeof(StringResources), nameof(StringResources.Error_weakPassword))]

Properties:

  • MinimumLenght (Default = 8)
  • MustContainSpecial (Default = true)
  • MustContainNumber (Default = true)
  • MustContainAlpha (Default = true)
  • MustContainUpperCase (Default = true)

Email Address

//Using the overload to use the localized string value.
[EmailAddress(typeof(StringResources), nameof(StringResources.Error_invalidEmailAddress))]

StringLength

//Using a hardcoded value.
[StringLength(2, "Field must be at least two characters long.")]

Constructor:

  • Minimum Length
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 646 4/23/2020
2.0.0 397 4/21/2020
1.0.3 573 3/15/2020
1.0.2 560 3/15/2020
1.0.1 565 3/15/2020
1.0.0 572 3/15/2020

Complete overhaul of the validation display system. No ErrorDisplay Attribute needed anymore. Validation Messages are showed directly in the corresponding entry. Validation is now truly async.