In this article, we are going to see how we can validate a data field by applying some validation rules in a Visual Studio Light Switch application. In our earlier article, we have seen how to handle the data with the different source provider options that are available with the Visual Studio Light Switch application in 2012 version. Light Switch application generates its own code base with the schema of the data and the relation while designing the tables. But with the front end screens which will be generated by itself in design time, we need to provide our own validation mechanism that best suits the business requirement for the specific field.
Light Switch validation can be done in three different ways as suggested by Microsoft which is of Entity validation, Screen validation and Data Service validation. Entity validation rules will validate each and every entity along with the properties that can be specified with the entity. Screen Validation rules will validate the screen properties and the data that are associated with the specific screen. Data Service Validation rules will validate the service and the data before storing the details to the storage medium. Let us see the steps on how to achieve this task in real time by providing a different validation rule for different fields in the application.
First level of Validation can be done at the design level itself by providing a required field option. To start with open Visual Studio 2012 IDE and create a new Light Switch Application with a valid project name in administrator mode as shown in the screen below.
Once the Project is created, let us start with designing the table (Check my previous articles to get clear idea on what a table is and a screen). Click on the Create table option and design your table with the columns as shown in the screen below.
In the Schema designer window, we can see the IsRequired field, which can be selected and omitted, based on the requirement. This is one of the validation (Called Declarative Validation) without having to write any code we can specify this validation. Now create a screen and run the application the specified field which is marked Isrequired will require the data to be filled else error will be thrown. In our case I will make StudName and Studage as IsRequired and run the application. We can see the error message as shown in the screen below:
Now let us add few more columns to the table, as Phone Number, Email ID and see how we can do a validation for these columns. Once we added the columns to the schema of the table click on the Properties windows and select the validation as shown in the screen below for Email Address validation.
Similarly for Phone number validation we can provide a number of formats as per your requirement as in different geography we have different formats as shown in the screen below.
Next validation can be we can specify the number of numeric value the phone number can accept or a SSN Number can accept or any unique identifier that we can predict as per our business requirement. In those cases we can able to make the validation with the numbers as shown in the screen below.
In order to do a Custom Validation we have an option at the bottom to make a custom validation by writing our own code with the available options by getting the Light Switch data and manipulate it with the traditional way of writing the conditions. To do that just click on the Custom validation at the bottom of the property window as shown in the screen below.
Now we can see a Class file opened with the respective validate event where we can write our code and validate it accordingly as shown in the screen below:
So in this article we have seen the different validation options and the rules that we can impose on developing our Visual Studio 2012 Light Switch Application with ease.