Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin
A plugin library that extends the functionality of FluentValidation for Xamarin.
https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin
fluentvalidation unobtrusive validation xamarin
Last synced: 30 days ago
JSON representation
A plugin library that extends the functionality of FluentValidation for Xamarin.
- Host: GitHub
- URL: https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin
- Owner: james-russo
- License: mit
- Created: 2017-09-14T21:47:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-30T19:47:12.000Z (over 5 years ago)
- Last Synced: 2024-11-02T20:42:11.812Z (about 1 month ago)
- Topics: fluentvalidation, unobtrusive, validation, xamarin
- Language: C#
- Homepage:
- Size: 8.36 MB
- Stars: 27
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-xamarin-forms - XamarinForms-UnobtrusiveValidationPlugin ★28
README
# Xamarin.Forms Unobtrusive FluentValidation Plugin
A plugin library that integrates the functionality of FluentValidation and Xamarin.Forms to provide an unobtrusive user experience.The library follows an MVVM pattern and below are examples on how to get started.
## Preview
![alt text](https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin/blob/master/samples/sample.gif "Sample In Action")
### Get Started
#### FluentValidation AbstractValidator (EnhancedAbstractValidator)
When using FluentValidation library, normally you would derive from *AbstractValidator*, however in this library you need to use **EnhancedAbstractValidator**.
This class extends AbstractValidator on and provides a new method **RuleForProp**. This method is essential in order for your view model later to work with the validator.
```csharp
public class PersonValidator : EnhancedAbstractValidator
{
public PersonValidator()
{
RuleForProp(a => a.FirstName)
.NotNull();
}
}
```
### ViewModel (AbstractValidationViewModel)
When deriving from AbstractValidationViewModel, you will want to the class **ValidatableProperty<>**. This property encapsulates the needed propertys for the UI to bind to accordingly.
```csharp
[FluentValidation.Attributes.Validator(typeof(PersonValidator))]
public class PersonViewModel : AbstractValidationViewModel
{
public ValidatableProperty FirstName { get; set; } = new ValidatableProperty();
}
```
### Xamarin.Forms UI Page
#### Code Page
```csharp
public class PersonPage : ContentPage
{
public PersonPage()
{
BindingContext = new PersonViewModel();//Here we pass in the name of the property we would like this control to bind to.
var firstNameControl = new ValidatableEntryControl(nameof(PersonViewModel.FirstName));var sl = new StackLayout()
{
Orientation = StackOrientation.Vertical
};sl.Children.Add(firstNameControl);
var submitButton = new Button()
{
Text = "Submit"
};//Sample to trigger validator, normally you would call the validator from within your view model.
submitButton.SetBinding(Button.CommandProperty, nameof(PersonViewModel.ValidateCommand));sl.Children.Add(submitButton);
this.Content = sl;
}}
```
#### XAML Page
```xml
Name
```