Validations in Silverlight using Data Annotation APIs
Validation is very important when designing the business applications. Silverlight provides a validation framework for validating the properties/data of the class. It is using annotation. Silverlight has different annotation to validate the property.
DLL: System.componentModel.DataAnnotation
Annotation attributes
[Range] : We can add range on the values of data. For example:
[Range(0, 5000)] public int ProductID { get; set; }
|
[Required] : We can mention if the data can be null or not.
[Required] public string ProductName { get; set; }
|
[RegularExpression] : We can set the pattern for the data. For example.
[RegularExpression(@”^[a-zA-Z”-‘\s]{1,40}$”, ErrorMessage = “Numbers and special characters are not allowed in the name.”)]
public string FirstName { get { return m_FirstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = “ FirstName “ }); m_FirstName = value; } }
|
[StringLength]: We can set the length for the string data. Min/Max etc.
[StringLength(8, MinimumLength = 3, ErrorMessage = “Last name must be between 3 and 8 characters long.”)]
public string LastName { get { return m_LastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = “LastName” }); m_LastName = value; } }
|
And many more….
Add inside XAML we should do following:
- Binding Mode should be TwoWay.
- We should set ValidatesOnExceptions=true and
- We should set NotifyOnValidationError=true.
For Example:
<TextBox x:Name=”tbIdNumber” Height=”23″ Width=”100″ Text=”{Binding IdNumber, Mode=TwoWay,ValidatesOnExceptions=true, NotifyOnValidationError=true}” /> |
Let’s Take an Example for above scenario:
Consider we have a class called Voter and we have following constraints Name should not be more than 20 characters, Name and Age should be provided. Age should be more than 18.
Steps:
- Add reference to “System.componentModel.DataAnnotation”
- Define a Voter class.
- Add Attributes on the properties as shown in the Voter class.
.cs file
public class Voter : INotifyPropertyChanged {
private string _name; private int _age; [Required] [StringLength(20,ErrorMessage=”Name length should not be more than 20.”)] public string Name { get { return _name;} set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = “Name” }); _name=value; OnPropertyChanged(“Name”); } } [Required] [Range(19,150)] public int Age { get { return _age; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = “Age” }); _age = value; OnPropertyChanged(“Age”); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } |
- Add XAML Content
- Add Two Way Binding, Add set ValidatesOnExceptions=true,NotifyOnValidationError=True.
XAML:
<UserControl x:Class=”Fir.Nomura.PortfolioAnalysis.Voter” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:BO=”clr-namespace:Fir.Nomura.PortfolioAnalysis.Business_Object” Width=”400″ Height=”400″ > <UserControl.Resources> <BO:Voter x:Name=”voterObj” /> </UserControl.Resources> <Grid x:Name=”LayoutRoot” Background=”White” DataContext=”{StaticResource voterObj}” > <Grid.ColumnDefinitions> <ColumnDefinition Width=”*”/> <ColumnDefinition Width=”*”/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height=”Auto”/> <RowDefinition Height=”Auto”/> </Grid.RowDefinitions> <TextBlock Text=”Name” Grid.Column=”0″ Grid.Row=”0″/> <TextBox Text=”{Binding Name,Mode=TwoWay,ValidatesOnExceptions=true,NotifyOnValidationError=True}” Grid.Row=”0″ Grid.Column=”1″ /> <TextBlock Text=”Age” Grid.Column=”0″ Grid.Row=”1″/> <TextBox Text=”{Binding Age,Mode=TwoWay,ValidatesOnExceptions=true,NotifyOnValidationError=True}” Grid.Row=”1″ Grid.Column=”1″ /> </Grid> </UserControl> |
Output Image:
Reference link:
http://msdn.microsoft.com/en-us/library/dd901590(v=vs.95).aspx
Leave a comment