Value Search

Tuesday, December 1, 2009

Data Validation in Silverlight 3



We take a look at the automatic and custom validation that you can have in data submitted to the server from your Silverlight 3 LoB application

We'll take a look at how you can add validation into the system so that there is both client and server side validation of the data that is sent back before it reaches the database.
The RIA SDK builds upon the LINQ model to deliver data both ways. This means that since the model is built from the database itself, some characteristics of the data schema are already present in the data model. This translates into some automatic validations that come into play by default.
For instance, if you have a field which is defined as a number, the user obviously should not be allowed to enter alphabets into it. Also, if you have defined the length of a particular field to be a particular value, the user should not enter strings longer than that. Luckily, such validations occur automatically in the DataForm control when bound to the DomainDataSource.
The DataForm control has built-in validation error displays that get triggered on the client end automatically when the data entered does not match the data model. The error is displayed in a number of ways:

  • A validation error summary block appears near the submit buttons that lets the user know of all the fields that caused errors along with the exact error message. Each error can be clicked to take the focus to the exact field.


  • Each field with an error is highlighted in the form in both the field entry outline as well as the name of the field.


  • On entering any error field (or by clicking a small arrow that appears on its side), that particular field's error message is shown next to it in a small popup tip.

All validations coming from the data model are very good. However, there is usually a need to add much more detailed level of validation into the model for individual fields that make up the data. The first way is to simply modify the metadata of the model itself.