Home > Articles > Programming > Visual Studio

  • Print
  • + Share This
This chapter is from the book

Default Validation of Business Types

In this chapter, you learned that Visual Studio LightSwitch introduces the concept of business data types. You also learned what business data types are about and why they are so useful. Continuing the discussion about data validation, business data types provide default validation mechanisms specific to their role. For instance, think of email addresses. These are not simply text; they are something requiring specific validation, such as the presence of a domain name or of the @ (at) symbol. LightSwitch provides a sophisticated validation mechanism that does the work for you, for all business data types. This section explains the default validation of the various business types.

Validating Email Addresses

The Email Address data type has a built-in validation mechanism that checks whether the supplied email address is well formed. Therefore, if the user enters an invalid email address, the application throws a notification reporting the error and prevents invalid changes from being saved. Figure 3.44 shows the validation error the user receives if a pound symbol (#) is entered rather than the required @.

Figure 3.44.

Figure 3.44. Default validation on Email Address data types.

After you fix the error, the validation test passes and you can save your changes.

Setting the Default Domain

The validation mechanism for the Email Address data type works against three properties: IsRequired and MaximumLength, which you already encountered when studying other primitive types; and Require Email Domain. When enabled, this last one requires the user to supply a valid domain name in the email address. For example, in my email address, the domain is @visual-basic.it. This can be quite annoying if you want to build a list of contacts from the same group of people, such as your colleagues, because in such a circumstance, all of your contacts’ email addresses have the same domain. Fortunately, the Email Address data type also provides the possibility of specifying a default domain that is automatically added to each email address. To provide a default domain, follow these steps:

  1. In the Entity Designer for the Contact entity, select the Email property and open the Properties window.
  2. Locate the Validation group and uncheck the Require Email Domain property.
  3. Locate the General group and, inside the Default Email Domain field, type the desired email domain. If you do not add the @ symbol at the beginning, LightSwitch adds it for you.

When you run the application next, you will notice that each time you add an email address, the specified domain is automatically added for you, saving a lot of time.

Validating Phone Numbers

Validation for properties of type Phone Number works as it does for strings, in that you can just specify whether the property is mandatory (IsRequired) and the length of the string representing the phone number (255 characters by default). More than for validation, the Phone Number data type is interesting for its customization and formatting methods, described earlier in this chapter.

Validating Images

The Image data type has just one validation property: IsRequired. This is because this type works in conjunction with controls that automatically determine whether an image is valid and can be accepted by properties of type Image.

Validating Money

The Money business data type is basically an evolution of the Decimal primitive type. In fact, their behavior is pretty identical; the difference is that Money provides the currency symbol and localized information according to the system’s regional settings. So, default validation for this type checks for the same properties as in Decimal: MinimumValue, MaximumValue, Precision, and Scale.

  • + Share This
  • 🔖 Save To Your Account