Share via


Adding Validation

This lesson shows how to validate the data that a user enters in a LightSwitch application.

Adding Data Validation

In almost any application that involves data input, the data must be validated before it is saved. For example, you might want to verify that a telephone number contains the correct number of digits or that a required field is not empty.

The Data Designer contains an example of one kind of validation: every entity field has a Required check box. If a field is marked as required and the field is empty, LightSwitch automatically displays a warning when the user tries to save the data.

You can also handle validation in LightSwitch by setting validation properties or by writing custom validation code.

To set validation properties

  1. In Solution Explorer, double-click Contacts.

  2. In the Data Designer, click <Add Property> and then type ContactState.

  3. Make sure that the type is String.

  4. In the Properties window, select the Maximum Length property and type 2.

    This setting assures that no more than two characters can be typed in the ContactState field.

To add custom validation code

  1. In Solution Explorer, double-click Order_Details.

  2. In the Data Designer, select the Quantity field.

  3. In the Properties widow, click the Custom Validation link.

    The Code Editor opens and a code block for the Quantity_Validate method is displayed.

    Writing validation code in the Code Editor

  4. Add the following code to the Quantity_Validate method just below the comment line.

    Warning

    Add either Visual Basic or C# code, depending on the programming language that was chosen when the project was created.

    If Quantity < 1 Then
        results.AddPropertyError("Quantity must be greater than zero.")
    End If
    
    if (Quantity < 1) 
    {
        results.AddPropertyError("Quantity must be greater than zero.");
    }
    

    This code assures that a value less than one in the Quantity field cannot be saved. If a user tries to save a value less than one, a validation error is triggered.

Closer Look

This lesson showed how to validate data by setting validation properties or by writing validation code. Different validation properties are available for different data types. For example, a String type has a Maximum Length property, and an Integer has Minimum Value and Maximum Value properties. All types have an Is Required property, which is the property that is used for the Required check box in the Data Designer.

If you examine the Customers entity, notice that the validation properties appear dimmed and cannot be changed. Some fields such as Address already have a value in the Maximum Length property. You cannot set validation properties in an entity from an attached data source; you can set them only in the entities that you create.

If you want to add validation for an entity from an attached data source, you can do so by writing your own validation code. You cannot override existing validation properties unless you are adding more restrictive validation. For example, for a field that has a Maximum Length of 30, you cannot add validation code to set the length to 40. However, you could add code to restrict the length to less than 30.

Next Steps

In the next lesson, you will learn how to add computed properties to data entities.

Next lesson: Adding Computed Properties

See Also

Tasks

How to: Validate Data

Other Resources

Working with Data in LightSwitch (Guided Tour)

How to: Validate the Entity Data Entities

Data: The Information Behind Your Application