diff --git a/Samples/Stylet.Samples.ModelValidation/Pages/UserView.xaml b/Samples/Stylet.Samples.ModelValidation/Pages/UserView.xaml index d90efe1..6dbd6c7 100644 --- a/Samples/Stylet.Samples.ModelValidation/Pages/UserView.xaml +++ b/Samples/Stylet.Samples.ModelValidation/Pages/UserView.xaml @@ -2,7 +2,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:s="https://github.com/canton7/Stylet" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> @@ -16,15 +17,17 @@ - - + + - - - + + + @@ -42,6 +45,7 @@ + @@ -55,5 +59,8 @@ + + Auto-Validate + diff --git a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs index 7cf29bd..a5eb750 100644 --- a/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs +++ b/Samples/Stylet.Samples.ModelValidation/Pages/UserViewModel.cs @@ -24,9 +24,21 @@ namespace Stylet.Samples.ModelValidation.Pages { } - public void ValidateModel() + protected override void OnValidationStateChanged() { - base.Validate(); + base.OnValidationStateChanged(); + // Fody can't weave other assemblies, so we have to manually raise this + this.NotifyOfPropertyChange(() => this.CanSubmit); + } + + public bool CanSubmit + { + get { return !this.AutoValidate || !this.HasErrors; } + } + public void Submit() + { + if (this.Validate()) + System.Windows.MessageBox.Show("Successfully submitted"); } } @@ -34,10 +46,10 @@ namespace Stylet.Samples.ModelValidation.Pages { public UserViewModelValidator() { - RuleFor(x => x.UserName).Length(1, 20); - RuleFor(x => x.Email).EmailAddress(); - RuleFor(x => x.Password).Matches("[0-9]").WithMessage("Must contain a number"); - RuleFor(x => x.PasswordConfirmation).Equal(s => s.Password).WithMessage("Should match Password"); ; + RuleFor(x => x.UserName).NotEmpty().Length(1, 20); + RuleFor(x => x.Email).NotEmpty().EmailAddress(); + RuleFor(x => x.Password).NotEmpty().Matches("[0-9]").WithMessage("{PropertyName} must contain a number"); + RuleFor(x => x.PasswordConfirmation).NotEmpty().Equal(s => s.Password).WithMessage("{PropertyName} should match Password"); } } }