Home » Blog

rounded header

Applying validations to LightSpeed models

tag icon Tagged as LightSpeed

LightSpeed 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.

kick it on DotNetKicks.com

Leave a Reply

Data Products Visual Controls Community Store
LightSpeed ORM
NHibernate Designer
SimpleDB Tools
SharePoint Tools
WPF Elements
WPF Diagrams
Silverlight Elements
Forums
Blog
Register
Login
Subscribe to newsletter
Buy Now
My Account
Volume Discounts
Purchase Orders
Contact Us