Building WP7 Custom Validation Control - Custom Validation Rules
published on: 4/22/2011 | Views: N/A | Tags: CustomControls UI
by WindowsPhoneGeek
This is the last post from the "Building WP7 Custom Validation Control " series of articles in which I talk about how to implement a fully functional, extensible and easy to use WP7 Validation Custom Control.
- Building WP7 Custom Validation Control - Architecture & Basic Prototype
- Building WP7 Custom Validation Control - Validation Logic
- Building WP7 Custom Validation Control - Custom Validation Rules
In this article I am going to talk about implementing Custom Validation Rules and how to integrate them with our previously created Validation Control..
NOTE: For reference take a look at the previous article: Building WP7 Custom Validation Control - Validation Logic
What is Validation Rule?
A validation rule is a piece of code that performs some check encapsulated in a reusable component so that you do not have to write it every time when you need to do input validation.
In order to implement a custom validation rule all you need to do is just to implement the IValidationRule interface and add the desired validation logic. We have described this interface in the previous article of this series.
Implementing LowerLettersValidationRule
Here is how we can easily implement a validation rule that allows only lower letters to be entered in the input area. Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class LowerLettersValidationRule : IValidationRule
{
public LowerLettersValidationRule()
{}
public bool Validate(string input)
{
if (input == null)
{
return true;
}
return input == input.ToLower();
}
}
Sample Usage
Just add an instance of your ValidationCntrol either in XAML or C# and after that set its ValidationRule property to the an instance of LowerLettersValidationRule.
Option1: Set ValidationRule with XAML only
<phone:PhoneApplicationPage.Resources>
<local:LowerLettersValidationRule x:Key="lowerCaseLetterValidationRule"/>
</phone:PhoneApplicationPage.Resources>
<validationControl:ValidationControl x:Name="validationLowerLetters" ValidationRule="{StaticResource lowerCaseLetterValidationRule}" ValidationContent="Only lower letters are allowed!"/>
Option2: Set ValidationRule with C#
<validationControl:ValidationControl x:Name="validationLowerLetters" ValidationContent="Only lower letters are allowed!">
<validationControl:ValidationControl.ValidationSymbol>
<Image Source="attention.png" />
</validationControl:ValidationControl.ValidationSymbol>
</validationControl:ValidationControl>
this.validationLowerLetters.ValidationRule = new LowerLettersValidationRule();
Implementing AndValidationRule
Here is how we can easily implement AndValidationRule that allows combining multiple simple rules into a complex one one using "and" logical operator.
Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class AndValidationRule : IValidationRule
{
public AndValidationRule()
{
this.Rules = new List<IValidationRule>();
}
public List<IValidationRule> Rules
{
get;
set;
}
public bool Validate(string input)
{
foreach (IValidationRule rule in this.Rules)
{
if (rule != null && !rule.Validate(input))
{
return false;
}
}
return true;
}
}
Sample Usage
Just add an instance of your ValidationCntrol either in XAML or C# and after that set its ValidationRule property to the an instance of AndValidationRule.
IValidationRule compositeRule1 = new AndValidationRule()
{
Rules = {
new MinLengthValidationRule() { MinLength = 4},
new MaxLenghtValidationRule() { MaxLength = 10 }
}
};
this.validationMinMaxLength.ValidationRule = compositeRule1;
Implementing OrValidationRule
Here is how we can easily implement OrValidationRule that allows combining multiple simple rules into a complex one one using "or" logical operator.
Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class OrValidationRule : IValidationRule
{
public OrValidationRule()
{
this.Rules = new List<IValidationRule>();
}
public List<IValidationRule> Rules
{
get;
set;
}
public bool Validate(string input)
{
foreach (IValidationRule rule in this.Rules)
{
if (rule != null && rule.Validate(input))
{
return true;
}
}
return false;
}
}
Sample Usage
Just add an instance of your ValidationControl either in XAML or C# and after that set its ValidationRule property to the an instance of OrValidationRule.
Option1: Set ValidationRule with XAML only
<phone:PhoneApplicationPage.Resources>
<local:AndValidationRule x:Key="complexMinMaxValidationRule">
<local:AndValidationRule.Rules>
<local:MinLengthValidationRule MinLength="4"/>
<local:MaxLenghtValidationRule MaxLength="10"/>
</local:AndValidationRule.Rules>
</local:AndValidationRule>
</phone:PhoneApplicationPage.Resources>
<validationControl:ValidationControl x:Name="validationMinMaxLengthWithXAML" ValidationRule="{StaticResource complexMinMaxValidationRule}" />
Option2: Set ValidationRule with C#
IValidationRule compositeRule2 = new OrValidationRule()
{
Rules = {
new FixedLenghtValidationRule() { Length = 5},
new FixedLenghtValidationRule() { Length = 7 }
}
};
this.validationFixedLength.ValidationRule = compositeRule2;
Implementing FixedLenghtValidationRule
Here is how we can easily implement a validation rule that restrict the length of the Text entered in the input area to a particular count(given number). Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class FixedLenghtValidationRule : IValidationRule
{
public int Length
{
get;
set;
}
public bool Validate(string input)
{
int inputLength = input != null ? input.Length : 0;
return inputLength == this.Length;
}
}
Sample Usage
Just add an instance of your ValidationCntrol either in XAML or C# . After that we will use OrValidationRule to combine a few FixedLenghtValidationRules into a complex validation rule.
Option1: Set ValidationRule with XAML only
<phone:PhoneApplicationPage.Resources>
<local:LowerLettersValidationRule x:Key="lowerCaseLetterValidationRule"/>
<local:FixedLenghtValidationRule x:Key="lowerFixedLengthValidationRule" Length="3"/>
</phone:PhoneApplicationPage.Resources>
<validationControl:ValidationControl x:Name="validationFixedLengthWithXAML" ValidationRule="{StaticResource lowerFixedLengthValidationRule}"/>
Option2: Set ValidationRule with C#
<validationControl:ValidationControl x:Name="validationFixedLength" ValidationContent="The length must be 5 or 7!"/>
IValidationRule compositeRule2 = new OrValidationRule()
{
Rules = {
new FixedLenghtValidationRule() { Length = 5},
new FixedLenghtValidationRule() { Length = 7 }
}
};
this.validationFixedLength.ValidationRule = compositeRule2;
Implementing MinLengthValidationRule
Here is how we can easily implement a validation rule that restrict the length of the Text entered in the input area to be greater than a particular length(given number). Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class MinLengthValidationRule : IValidationRule
{
public int MinLength
{
get;
set;
}
public bool Validate(string input)
{
int inputLength = input != null ? input.Length : 0;
return inputLength >= this.MinLength;
}
}
Implementing MaxLenghtValidationRule
Here is how we can easily implement a validation rule that restrict the length of the Text entered in the input area to be less than a particular length(given number). Note that we perform the actual validation in the Validate method which returns true the text is valid otherwise returns false.
public class MaxLenghtValidationRule : IValidationRule
{
public int MaxLength
{
get;
set;
}
public bool Validate(string input)
{
int inputLength = input != null ? input.Length : 0;
return inputLength <= this.MaxLength;
}
}
Sample Usage
Just add an instance of your ValidationCntrol either in XAML or C# . After that we will use AndValidationRule to combine MinLengthValidationRule and MaxLenghtValidationRule into a complex validation rule.
<validationControl:ValidationControl x:Name="validationMinMaxLength" ValidationContent="The Min length must be 4 and the Max 10 symbols!">
<validationControl:ValidationControl.ValidationSymbol>
<Image Source="attention.png" />
</validationControl:ValidationControl.ValidationSymbol>
</validationControl:ValidationControl>
IValidationRule compositeRule1 = new AndValidationRule()
{
Rules = {
new MinLengthValidationRule() { MinLength = 4},
new MaxLenghtValidationRule() { MaxLength = 10 }
}
};
this.validationMinMaxLength.ValidationRule = compositeRule1;
That was all about implementing Custom Validation Riles Silverlight for Windows Phone 7.
You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
