Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 14, 2018 04:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/2fc03355bc31e6a79d9a14389f168d7c to your computer and use it in GitHub Desktop.
Save danielplawgo/2fc03355bc31e6a79d9a14389f168d7c to your computer and use it in GitHub Desktop.
Integracja Fluent Validation z WPF
<Window x:Class="FluentValidationWithWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FluentValidationWithWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBox Text="{Binding Email, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}"
Name="Email"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Errors: " />
<TextBlock Text="{Binding ElementName=Email, Path=(Validation.Errors)[0].ErrorContent}" />
</StackPanel>
<Button Content="Save"
Command="{Binding SaveCommand}"/>
</StackPanel>
</Window>
public class MainWindowViewModel : ViewModel, IMainWindowViewModel
{
protected MainWindowViewModelValidator Validator { get; set; }
public MainWindowViewModel(IMainWindow view)
: base(view)
{
Validator = new MainWindowViewModelValidator();
}
private string _email;
public string Email
{
get { return _email; }
set
{
if (_email != value)
{
_email = value;
OnPropertyChanged(() => this.Email);
SaveCommand.RaiseCanExecuteChanged();
}
}
}
private DelegateCommand _saveCommand;
public DelegateCommand SaveCommand
{
get
{
if(_saveCommand == null)
{
_saveCommand = new DelegateCommand(Save, () => IsValid);
}
return _saveCommand;
}
}
public void Save()
{
}
public override ValidationResult SelfValidate()
{
return Validator.Validate(this);
}
}
public class MainWindowViewModelValidator : AbstractValidator<MainWindowViewModel>
{
public MainWindowViewModelValidator()
{
RuleFor(u => u.Email)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.EmailAddress();
}
}
public virtual ValidationResult SelfValidate()
{
return new ValidationResult();
}
private string GetValidationError(string property = null)
{
var validationResult = SelfValidate();
if (validationResult == null || validationResult.IsValid)
{
IsValid = true;
return string.Empty;
}
IsValid = false;
if (property == null)
{
return string.Join(Environment.NewLine, validationResult.Errors.Select(e => e.ErrorMessage));
}
else
{
var results = validationResult.Errors.FirstOrDefault(e => e.PropertyName == property);
return results != null ? results.ErrorMessage : string.Empty;
}
}
public string Error
{
get
{
return GetValidationError();
}
}
public string this[string property]
{
get
{
return GetValidationError(property);
}
}
public bool IsValid { get; private set; }
protected bool Validate()
{
IsValid = SelfValidate().IsValid;
return IsValid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment