One of the key features that need to be implemented in a Line of Business application. Although Silverlight supports validation very well it’s not that easy compared to enabling Data Binding your ViewModel. When we want our ViewModel to be fully Data Bindable we implement the INotifyPropertyChanged interface. It’s really an easy interface with an Event that needs to be called when the value of a property has changed. A lot of people don’t implement this interface themselves but use a framework like MVVM Light. I’m using that as well.
To make the validation work in a similar way we will have to implement INotifyDataErrorInfo. This is similar but I haven’t found a framework implementing it for me, so I wrote my own implementation of a ViewModelBase that both supports INotifyPropertyChanged and INotifyDataErrorInfo. Declaring the validation rules
Now that we have a ViewModel which supports validation we need to use it in our ViewModels. Let’s say that we have a LoginViewModel, which requires a Username and Password to be filled in. So we need to declare our properties Username and Password as Required. Besides that we also want the message text to be changed to something different than the default, so we set the ErrorMessage property.public class Login : ViewModelBase
{
private string _password;
private bool _rememberMe;
private string _userName; [Required(AllowEmptyStrings = false, ErrorMessage = "Username is required")]
public string UserName
{
get { return _userName; }
set
{
if (_userName != value)
{
ValidateProperty("UserName", value);
_userName = value;
base.RaisePropertyChanged("UserName");
}
}
} [Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
public string Password
{
get { return _password; }
set
{
if (_password != value)
{
ValidateProperty("Password", value);
_password = value;
base.RaisePropertyChanged("Password");
}
}
} public bool RememberMe
{
get { return _rememberMe; }
set
{
if (_rememberMe != value)
{
_rememberMe = value;
base.RaisePropertyChanged("RememberMe");
}
}
}
}Alright this was the usage of one of the most rudimentary validation types. A small overview of all the different supported validation types.
- RequiredAttribute used to check if a property has a value and will return invalid in case of null, empty string, or only white space characters.
- DataTypeAttribute like it’s name the data type validator validates a value against certain ‘data types’: DateTime, Date, Time, Duration, PhoneNumber, Currency, Text, Html, MultilineText, EmailAddress, Password, Url, ImageUrl.
- RangeAttribute is used to validate int, decimal and double values which need to fall between certain min and max values.
- RegularExpressionAttribute can be used for more advanced scenario’s where you can write the full regular expression which should be used to validate against.
- StringLengthAttribute can be used to validate the length of a string in characters, you can specify both the maximum length and minimum length, but one of them is also possible. Be aware that a null value will be seen as valid!
- CustomValidationAttribute used in the most complex validation scenario’s. The CustomValidationAttribute needs to be used in conjunction with a static method which returns a ValidationResult. This static method can be used to implement your very complex validation rules. And if the above validation types don’t fit, you can always derive from the ValidationAttribute and write your own implementation. It’s important to know that in case of a null-value the properties won’t be validated. This is something that’s not always helpful, specially in case of the CustomValidation where you want to implement a conditional required field validation. The Required validation does get fired in case of null-value. Read more: Mark Monster
To make the validation work in a similar way we will have to implement INotifyDataErrorInfo. This is similar but I haven’t found a framework implementing it for me, so I wrote my own implementation of a ViewModelBase that both supports INotifyPropertyChanged and INotifyDataErrorInfo. Declaring the validation rules
Now that we have a ViewModel which supports validation we need to use it in our ViewModels. Let’s say that we have a LoginViewModel, which requires a Username and Password to be filled in. So we need to declare our properties Username and Password as Required. Besides that we also want the message text to be changed to something different than the default, so we set the ErrorMessage property.public class Login : ViewModelBase
{
private string _password;
private bool _rememberMe;
private string _userName; [Required(AllowEmptyStrings = false, ErrorMessage = "Username is required")]
public string UserName
{
get { return _userName; }
set
{
if (_userName != value)
{
ValidateProperty("UserName", value);
_userName = value;
base.RaisePropertyChanged("UserName");
}
}
} [Required(AllowEmptyStrings = false, ErrorMessage = "Password is required")]
public string Password
{
get { return _password; }
set
{
if (_password != value)
{
ValidateProperty("Password", value);
_password = value;
base.RaisePropertyChanged("Password");
}
}
} public bool RememberMe
{
get { return _rememberMe; }
set
{
if (_rememberMe != value)
{
_rememberMe = value;
base.RaisePropertyChanged("RememberMe");
}
}
}
}Alright this was the usage of one of the most rudimentary validation types. A small overview of all the different supported validation types.
- RequiredAttribute used to check if a property has a value and will return invalid in case of null, empty string, or only white space characters.
- DataTypeAttribute like it’s name the data type validator validates a value against certain ‘data types’: DateTime, Date, Time, Duration, PhoneNumber, Currency, Text, Html, MultilineText, EmailAddress, Password, Url, ImageUrl.
- RangeAttribute is used to validate int, decimal and double values which need to fall between certain min and max values.
- RegularExpressionAttribute can be used for more advanced scenario’s where you can write the full regular expression which should be used to validate against.
- StringLengthAttribute can be used to validate the length of a string in characters, you can specify both the maximum length and minimum length, but one of them is also possible. Be aware that a null value will be seen as valid!
- CustomValidationAttribute used in the most complex validation scenario’s. The CustomValidationAttribute needs to be used in conjunction with a static method which returns a ValidationResult. This static method can be used to implement your very complex validation rules. And if the above validation types don’t fit, you can always derive from the ValidationAttribute and write your own implementation. It’s important to know that in case of a null-value the properties won’t be validated. This is something that’s not always helpful, specially in case of the CustomValidation where you want to implement a conditional required field validation. The Required validation does get fired in case of null-value. Read more: Mark Monster
0 comments:
Post a Comment