Permalink
Browse files

Cleaned up and set allErrors more efficiently.

  • Loading branch information...
Thorsten Lorenz
Thorsten Lorenz committed Jul 27, 2010
1 parent a03e7bb commit b4c95aa189d081bde4d63d006065e46e39a9bc4f
@@ -17,10 +17,7 @@ public class DataErrorInfoProvider : IDataErrorInfoProvider
public IList<string> AllErrors
{
- get
- {
- return _allErrors;
- }
+ get { return _allErrors; }
}
public IDictionary<string, IList<string>> ErrorsForProperty
@@ -64,6 +61,7 @@ public string Validate(string propertyName)
_errorsForProperty[propertyName].Clear();
if (_validations.ContainsKey(propertyName))
+ {
_validations[propertyName].ForEach(getValidation => {
var validation = getValidation();
@@ -73,14 +71,12 @@ public string Validate(string propertyName)
{
if (!_errorsForProperty[propertyName].Contains(descriptionWithName))
_errorsForProperty[propertyName].Add(descriptionWithName);
-
- if (!_allErrors.Contains(descriptionWithName))
- _allErrors.Add(descriptionWithName);
}
- else
- _allErrors.Remove(descriptionWithName);
});
+ UpdateAllErrors();
+ }
+
return _errorsForProperty[propertyName].IsNotEmpty()
? _errorsForProperty[propertyName].Aggregate((errorsSoFar, extraErrors) => errorsSoFar += extraErrors + "\n")
: null;
@@ -95,5 +91,11 @@ public string ValidateAll()
? _allErrors.Aggregate((errorsSoFar, extraErrors) => errorsSoFar += extraErrors + "\n")
: null;
}
+ void UpdateAllErrors()
+ {
+ _allErrors.Clear();
+ _errorsForProperty.Values
+ .ForEach(items => items.ForEach(_allErrors.Add));
+ }
}
}
@@ -37,7 +37,7 @@ public static IValidation NeedsToStartWithCapitalLetter(this string property)
public static IValidation MustHaveMinimumLengthOf(this string property, int length)
{
- return new Validation("{0} must be at least " + length +" letters long!",
+ return new Validation("{0} must be at least " + length +" letters long",
property.Trim().Length >= length);
}
@@ -19,6 +19,7 @@ public class ValidatingViewModel : NotifyPropertyChanged, IDataErrorInfo
IDataErrorInfoProvider _dataErrorInfoProvider;
string _firstName;
string _lastName;
+ ICommand _submitCommand;
public ValidatingViewModel(IDataErrorInfoProvider dataErrorInfoProvider)
{
@@ -73,13 +74,6 @@ public string LastName
}
}
- public string this[string propertyName]
- {
- get { return _dataErrorInfoProvider.Validate(propertyName); }
- }
-
- ICommand _submitCommand;
-
public ICommand SubmitCommand
{
get
@@ -92,6 +86,11 @@ public ICommand SubmitCommand
}
}
+ public string this[string propertyName]
+ {
+ get { return _dataErrorInfoProvider.Validate(propertyName); }
+ }
+
void SetupValidations()
{
_dataErrorInfoProvider.AddValidations(() => FirstName,
@@ -105,7 +104,7 @@ void SetupValidations()
() => LastName.CanOnlyContainLetters(),
() => LastName.NeedsToStartWithCapitalLetter(),
() => LastName.CannotHaveLeadingWhiteSpaces(),
- () => new Val("{0} cannot be the same as the first name!", LastName.IsEmpty() || !LastName.Equals(FirstName)));
+ () => new Val("{0} cannot be the same as the first name", LastName.IsEmpty() || !LastName.Equals(FirstName)));
_dataErrorInfoProvider.AddValidations(() => Age,
() => Age.MustBeGreaterOrEqualTo(18),
@@ -1,8 +1,17 @@
<Window x:Class="Validation.Views.ValidatingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="ValidatingView" Height="500" Width="900">
+ Title="ValidatingView" Height="500" Width="900"
+ >
+
<Grid>
+ <Grid.Resources>
+ <Style TargetType="{x:Type ListView}">
+ <Setter Property="HorizontalAlignment" Value="Stretch" />
+ <Setter Property="Background" Value="AntiqueWhite" />
+ <Setter Property="TabIndex" Value="9999" />
+ </Style>
+ </Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="150" />
<RowDefinition Height="*" />
@@ -13,27 +22,27 @@
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
-
+
<GroupBox Grid.Row="0" Grid.Column="0" Header="First Name">
<StackPanel>
<TextBox Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" Margin="5" />
- <ListView ItemsSource="{Binding Errors[FirstName], Mode=OneWay}" HorizontalAlignment="Stretch" />
+ <ListView ItemsSource="{Binding Errors[FirstName], Mode=OneWay}" />
</StackPanel>
</GroupBox>
<GroupBox Grid.Row="0" Grid.Column="1" Header="Last Name">
<StackPanel>
<TextBox Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" Margin="5" />
- <ListView ItemsSource="{Binding Errors[LastName], Mode=OneWay}" HorizontalAlignment="Stretch" />
+ <ListView ItemsSource="{Binding Errors[LastName], Mode=OneWay}" />
</StackPanel>
</GroupBox>
<GroupBox Grid.Row="0" Grid.Column="2" Header="Age">
<StackPanel>
<TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true}" Margin="5" />
- <ListView ItemsSource="{Binding Errors[Age], Mode=OneWay}" HorizontalAlignment="Stretch" />
+ <ListView ItemsSource="{Binding Errors[Age], Mode=OneWay}" />
</StackPanel>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Header="Error Summary">
- <ListView ItemsSource="{Binding AllErrors, Mode=OneWay}" HorizontalAlignment="Stretch" />
+ <ListView ItemsSource="{Binding AllErrors, Mode=OneWay}" />
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="2" Header="Confirmation" >
<Button Content="Submit" Command="{Binding SubmitCommand}" VerticalAlignment="Bottom" />

0 comments on commit b4c95aa

Please sign in to comment.