More work on Stylet.Samples.ModelValidation

This commit is contained in:
Antony Male 2014-05-13 13:29:02 +01:00
parent 9dcce8e164
commit 779df6ebcc
2 changed files with 31 additions and 12 deletions

View File

@ -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">
<UserControl.Resources>
@ -16,15 +17,17 @@
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Bottom" Foreground="OrangeRed" Text="{Binding ElementName=CustomAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<DockPanel LastChildFill="False">
<TextBlock DockPanel.Dock="Bottom" HorizontalAlignment="Left" Width="{Binding ElementName=CustomAdorner, Path=ActualWidth}"
Foreground="OrangeRed" TextTrimming="WordEllipsis"
Text="{Binding ElementName=CustomAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<Border Background="OrangeRed" DockPanel.Dock="Right" Margin="5,0,0,0" VerticalAlignment="Top"
Width="20" Height="20" CornerRadius="5">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white" />
</Border>
<AdornedElementPlaceholder Name="CustomAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
<Border BorderBrush="red" BorderThickness="1">
<AdornedElementPlaceholder Name="CustomAdorner" VerticalAlignment="Center"/>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
@ -42,6 +45,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Target="{Binding ElementName=UserName}">UserName:</Label>
@ -55,5 +59,8 @@
<Label Grid.Row="3" Grid.Column="0" Target="{Binding ElementName=PasswordConfirmation}">Password Confirmation:</Label>
<TextBox Grid.Row="3" Grid.Column="1" x:Name="PasswordConfirmation" Text="{Binding PasswordConfirmation}"/>
<CheckBox Grid.Row="4" Grid.Column="0" IsChecked="{Binding AutoValidate}">Auto-Validate</CheckBox>
<Button Grid.Row="4" Grid.Column="1" Command="{s:Action Submit}" HorizontalAlignment="Left">Submit</Button>
</Grid>
</UserControl>

View File

@ -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");
}
}
}