Now with Plugin.ValidationRules we can validate our list in Xamarin.Forms. We have a new update Plugin.ValidationRules now with its version 1.4 has a lot of new features to speed up our work. This release has many quality improvements and new features added, including a new Errors support for ValidationUnit, among other things.
You can download the new Plugin.ValidationRules in Nuget Marketplace.
Upgrading to 1.4
This update only has one breaking change that forces you to change your code when you update, rather it comes with many new features that will help you optimize your time. In short, there’s no abrupt change, if you upgrade now, everything should keep working the same for you.
Breaking changes
- Change ValueFormatter to Formatter for Validatable object.
Phone.Formatter = new MaskFormatter("XXX-XXX-XXXX");
New features
- New ValidatableList object added for validation lists with validation rules.
- New ValidatorList Builder added for ValidatableList object.
... var monkeyList = new List<string>(); monkeyList.Add("Baboon"); monkeyList.Add("Capuchin Monkey"); monkeyList.Add("Blue Monkey"); MonkeyList = ValidatorList.Build<string>() .AddItemsSource(monkeyList) .IsRequired("An item is required.") .Must(value => { if(MonkeyList.SelectedIndex == 2 && Quantity.Value < 5) // == blue monkey return false; return true; }, "Minimum quantity of this monkey is 5"); ... public ValidatableList<string> MonkeyList { get; set; } ...
<Picker Title="Select the monkey" ItemsSource="{Binding MonkeyList.ItemsSource}" SelectedIndex="{Binding MonkeyList.SelectedIndex}" /> <Label HorizontalTextAlignment="Center" Text="{Binding Path=MonkeyList.Error}" TextColor="Red" />
- New UnFormat method added for IValueFormatter interface.
- Errors support added for ValidationUnit object.
ValidationUnit UnitValidation = new ValidationUnit(Quantity, MonkeyList); UnitValidation.Validate(); var error = UnitValidation.Error;
- New ValueFormatted property added for Validatable object.
<Entry x:Name="phoneEntry" Placeholder="Phone" Text="{Binding Phone.ValueFormatted, Mode=TwoWay}" /> <Label HorizontalTextAlignment="Center" Text="{Binding Phone.Error}" TextColor="Red" />
Others
- Some improvements were added.
- Some minor issues were fixed.
Resources, Documentation, Examples and Videos
See the release change notes for full details on what’s new in the release. You can also review the initial and complementary documentation that is very useful.
You can also see the documentation from the previous version because it has a lot of new features including the auto mapper for our Model Validators.
I don’t think new documentation is needed, as the new features only support what had already been proposed.
The Xamarin.Forms sample can be reviewed from today and is updated with the new Plugin.ValidationRules version.
Videos
Take a look at this presentation as an initial game to understand what the plugin does. You can find it on YouTube.
Conclusion
The truth is that I am very happy with this update, and it is that once you start using the plugin it is very difficult for you to change it or try to use something else.
Update the plugin to 1.4 through NuGet Package Manager. Let me know how you’re doing! And if you have any problems, please create an Issue or write to me on Twitter @luismatosluna.