Applying validations to LightSpeed models
Tagged as LightSpeedLightSpeed allows you to specify constraints on entity attribute values. For example, on a Person entity, you might specify that Name must be present, that Email must be in email address format and HomePage in URL format, and that Age must be in the range 0 to 150. You specify these constraints in your entity code by placing attributes on the entity fields.
If you’re using the LightSpeed designer, it generates the field declarations for you, so you need to specify the validations in your model. There are two ways to do this: the “real” way which gives you access to the full validation capabilities of LightSpeed, and a shortcut way which you may find quicker and more convenient once you know how to use it.
The “real” way uses the LightSpeed Model Explorer, the Solution Explorer-like tree that shows you the entire contents of your model. If you can’t see this window, open a LightSpeed model, and go to View > Other Windows > LightSpeed Model. Expand the tree until you find the property you want to validate. Now right-click it and add the validation or validations you want.

The validations for a property are listed under its Validations folder in the LightSpeed Model Explorer. Click on a validation in the Explorer to edit its details in the Properties window.

A lot of the time you don’t need this level of fine control over validations. So the designer also offers shortcuts: if you select a property, the Properties window displays a number of validation options. Most of these are self-explanatory — for example you can use the Validate Presence property to toggle presence validation on or off. Validate Length and Validate Value are a little more complex: they use a C#-like syntax, which is described in the User Guide under Help Topics > Generating LightSpeed Models. Here are our Message validations being edited via their shortcuts:

The main limitation of the shortcuts is that you can’t specify a custom error message. However, all the shortcuts actually do is display, create and edit validations in the tree: for example if you toggle Validate Email to True in the Properties window, behind the scenes this just adds an Email Validation in the tree. So if you create a validation using a shortcut, you can always use the Model Explorer to edit it later and add a custom error message. Also, there’s no shortcut for custom validations; you have to create those via the Model Explorer. (There are also some edge cases that you might be able to represent with a combination of Range and Value validations in the tree, but can’t express using the Validate Value shortcut. For example, value must be between -10 and +10, but must not equal 0. We think these are more theoretical than actual, though.)
Whichever way you edit the validations, the result is the same: the appropriate attributes are placed against the generated field in the entity code, and the LightSpeed validation infrastructure does the rest.
Want to try it out? LightSpeed Express is free, even for commercial use — download it and have a go.
Leave a Reply
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (27)
Lab Samples (2)
LightSpeed (112)
MegaPack (3)
News (37)
Products (51)
Projects (4)
Screencast (6)
Silverlight (1)
Silverlight Elements (1)
SimpleDB Management Tools (8)
Visual Studio (4)
VS File Explorer (4)
WPF (28)
WPF Diagramming (11)
WPF Elements (15)
WPF Property Grid (20)
![]()
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 Ivan Towlson on 15 June 2008 


