Skip to content

Instantly share code, notes, and snippets.

@TBertuzzi
Created November 3, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TBertuzzi/9acfadc7cbb48716ceeb38b094e3edea to your computer and use it in GitHub Desktop.
Save TBertuzzi/9acfadc7cbb48716ceeb38b094e3edea to your computer and use it in GitHub Desktop.
Fluent Validation
using System;
using System.Windows.Input;
using FluentValidation;
using MvvmHelpers;
using Xamarin.Forms;
using XamarinFluentValidation.Models;
using XamarinFluentValidation.Models.Validators;
namespace XamarinFluentValidation.ViewModels
{
public class MainViewModel : BaseViewModel
{
private Pessoa _pessoa;
public Pessoa Pessoa
{
get { return _pessoa; }
set { SetProperty(ref _pessoa, value); }
}
public ICommand ValidarCommand { get; set; }
private PessoaValidator _validator;
public MainViewModel()
{
Pessoa = new Pessoa();
_validator = new PessoaValidator();
ValidarCommand = new Command(ValidarCommandExecute);
}
void ValidarCommandExecute()
{
var result = _validator.Validate(Pessoa);
if (result.IsValid)
{
App.Current.MainPage.DisplayAlert("Cadastro", "Cadastro Validado com Sucesso", "Ok");
}
else
{
var erros = "";
foreach (var failure in result.Errors)
{
erros += $",{failure.ErrorMessage}";
}
App.Current.MainPage.DisplayAlert("FluentValidation", erros.Substring(1), "Ok");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment