Validation with LightSpeed
Tagged as LightSpeedValidation is an important part of most applications, it helps ensure data integrity and, if implemented well, guide the user on how to use applications correctly in a friendly manner.
A common query about how to achieve such validation is where the validation should occur and how it should be applied. Many developers elect to place such validation code close to the view, for example in ASP.NET you may use the Validator controls and have any custom validation in the code behind of the page. Modern software development is moving to place field level validation in the domain model to provide a single instance of these rules to avoid duplication. LightSpeed provides a rich framework to help create these validations.
LightSpeed ships with several common validators (found under the Mindscape.LightSpeed.Validation namespace).
- Presence validator: ensure a field has a value
- Unique validator: ensure a field is unique in the database
- Uri validator: ensure a field is a valid URI
- Format validator: ensure a field conforms to a provided regular expression
- Email validator: ensure a field is a valid email address
- Comparison validator: compares a value to another value
More than one validator can be applied to a single field which enables richer validation if required.
Applying a validation
Validators in LightSpeed are applied as attributes to the fields in the domain classes. For example, to apply the presence validation to a firstname field you would write the following:
[ValidatePresence] private string _firstname;
To apply multiple validators:
[ValidateUnique] [ValidateEmailAddress] private string _emailAddress;
Validating objects
Once validation attributes have been applied you will no longer be able to persist objects that fail validation. The exception thrown will provide details of what validation failed. That works well for ensuring data consistency however this does not help create an enjoyable experience for end users. Thankfully, you can validate domain objects by calling the .Validate() method on the object.
Once validation has occurred, if any have failed, the Errors collection on the object will be populated with friendly error messages about why the object is invalid. This provides a elegant approach to validating forms such as a sign-up web page:
- Create the sign-up form
- On post back assign each field to the properties of the user object
- Call Validate() on the object
- If validation fails bind the Errors collection to a repeater or similar on the web page
- If validation passes, persist the object
Further to this, every LightSpeed object implements the IDataErrorInfo interface which most .NET controls can use to show in real time if an object that is bound to it is valid. For example, if binding LightSpeed objects to a Grid control the red error icon will display in the field if the object is in an invalid state and provide a tool-tip describing the fault.
Create your own validator
LightSpeed makes the ValidateAttribute class available for developers to create their own validators (by creating new ValidationRule objects). This can be useful if a custom validator is needed in several model classes, allowing reuse of your custom validation code.
Alternatively, if custom validation is required for a given object, LightSpeed allows developers to override the OnValidate() method of the class:
protected override void OnValidate() { if ((Contributor == null) && (ApprovedBy == null)) { Errors.AddError("Must have one or the other"); } }
For more information regarding Validation in LightSpeed please read the Validation section of the user guide.
I hope this quick post about domain model validation has been useful in helping you write more robust solutions using LightSpeed. If you feel anything has been left out or have any questions please leave a comment.
John-Daniel Trask
2 Responses to “Validation with LightSpeed”
Leave a Reply
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (31)
Lab Samples (2)
LightSpeed (132)
MegaPack (3)
News (48)
Products (64)
Projects (4)
Screencast (6)
SharePoint (1)
Silverlight (5)
Silverlight Elements (12)
SimpleDB Management Tools (11)
Visual Studio (4)
VS File Explorer (5)
WPF (31)
WPF Diagramming (14)
WPF Elements (22)
WPF Property Grid (24)
![]()
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007





Posted by John-Daniel Trask on 29 August 2007



Hi,
Can this be used in .net 1.1 projects.
Cheers
A
Sorry LightSpeed is 2.0 and up.
Cheers,
Andrew.