# HG changeset patch # User Steven Hollidge # Date 1334811568 -3600 # Node ID 02b1513f679a5d69d2c0e6390dc6034ce3080e62 # Parent 64f19743cfd695ba9aa4162972a2c552f1d8bd59 finished silverlight validation diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/App.xaml --- a/SilverlightValidation/SilverlightValidation/App.xaml Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/App.xaml Thu Apr 19 05:59:28 2012 +0100 @@ -1,3 +1,460 @@  + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:p="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls" + xmlns:s="clr-namespace:System;assembly=mscorlib" + xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/RelayCommand.cs --- a/SilverlightValidation/SilverlightValidation/RelayCommand.cs Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/RelayCommand.cs Thu Apr 19 05:59:28 2012 +0100 @@ -5,7 +5,7 @@ { public class RelayCommand : ICommand { - public event EventHandler CanExecuteChanged; + public event EventHandler CanExecuteChanged = delegate { }; readonly Action _execute; readonly Predicate _canExecute; @@ -22,8 +22,7 @@ public void UpdateCanExecuteCommand() { - if (CanExecuteChanged != null) - CanExecuteChanged(this, new EventArgs()); + CanExecuteChanged(this, new EventArgs()); } diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj --- a/SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj Thu Apr 19 05:59:28 2012 +0100 @@ -64,6 +64,7 @@ ..\Libs\FluentValidation.dll + diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/UserModel.cs --- a/SilverlightValidation/SilverlightValidation/UserModel.cs Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserModel.cs Thu Apr 19 05:59:28 2012 +0100 @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; namespace SilverlightValidation { @@ -12,9 +13,14 @@ public string Username { get; set; } public string Email { get; set; } public string Password { get; set; } - public DateTime DateOfBirth { get; set; } + public DateTime? DateOfBirth { get; set; } public string Description { get; set; } + public static UserModel Create() + { + return new UserModel() { Username = "", Email = "", Password = "", DateOfBirth = null, Description = "" }; + } + public UserModel Clone() { return (UserModel) this.MemberwiseClone(); diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/UserModelValidator.cs --- a/SilverlightValidation/SilverlightValidation/UserModelValidator.cs Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserModelValidator.cs Thu Apr 19 05:59:28 2012 +0100 @@ -8,25 +8,28 @@ public UserModelValidator() { RuleFor(x => x.Username) - .NotEmpty() - .WithMessage("Username cannot be blank."); + .Length(3, 8) + .WithMessage("Must be between 3-8 characters."); RuleFor(x => x.Password) - .NotEmpty() - .WithMessage("Password cannot be blank."); + .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$") + .WithMessage("Must contain at least one lower case, one upper case and a numeric character."); RuleFor(x => x.Email) .EmailAddress() - .WithMessage("Email is required."); + .WithMessage("A valid email address is required."); RuleFor(x => x.DateOfBirth) .Must(BeAValidDateOfBirth) - .WithMessage("Date of birth must be within the last 100 years."); + .WithMessage("Must be within 100 years of today."); } - private bool BeAValidDateOfBirth(DateTime dateOfBirth) + private bool BeAValidDateOfBirth(DateTime? dateOfBirth) { - return (dateOfBirth < DateTime.Today) && (dateOfBirth > DateTime.Today.AddYears(-100)); + if (dateOfBirth == null) return false; + if (dateOfBirth.Value > DateTime.Today || dateOfBirth < dateOfBirth.Value.AddYears(-100)) + return false; + return true; } } } diff -r 64f19743cfd6 -r 02b1513f679a SilverlightValidation/SilverlightValidation/UserView.xaml --- a/SilverlightValidation/SilverlightValidation/UserView.xaml Wed Apr 18 22:28:46 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/UserView.xaml Thu Apr 19 05:59:28 2012 +0100 @@ -3,9 +3,11 @@ 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:p="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls" + xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" d:DesignHeight="350" - d:DesignWidth="400" + d:DesignWidth="450" mc:Ignorable="d"> @@ -18,92 +20,131 @@ - + + - - - - - - - - - - - - - + + + + + + + + + + - - + + + + + ValidatesOnDataErrors=True, + NotifyOnValidationError=True}" + Style="{StaticResource DatePickerStyle}" /> + + + + + -