Created
November 3, 2020 19:27
-
-
Save TBertuzzi/9acfadc7cbb48716ceeb38b094e3edea to your computer and use it in GitHub Desktop.
Fluent Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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