Tag Archive: data validation in silverlight 4.0


In my last blog post I had explained the two standard ways of  validating data in Silverlight 4. One way was implementing “INotifyProprtyChanged” interface. And the second way was “INotifyDataErrorInfo” interface, which is the new option available with Silverlight 4 for validating data.

Now in this blog post I will show, how we can use the validation asynchronously with “INotifyDataErrorInfo” interface in Silverlight 4.

Simple Scenario where Asynchronous Validation can be useful:

Assume that we are just registering new users for a website accepting username and password in Silverlight front-end UI. Now when user enters the preffered username, we need to check that preffered username is available or not. So we just call the web service for checking the availability of preffered username. But as we are using Asynchronous Data Validation to perform this username validation, and so while the web service returns the response, Silverlight front-end UI still remains available for User for entering password and so on……….

Here is the Entity class “User” which implements the “INotifyDataErrorInfo” interface as shown in the image below……

Here is the property “username” in the entity class “User” which is getting validated asynchronously…….

In this code “WebService.CheckUserNameAvailability(string userName, UserNameCallBack callBackMethod)” accepts userName as first parameter and CallBackMethod for UserNameCallBack delegate as a second parameter. So Web Service returns the response by calling this callBackDalegateMethod and passing “isUserNameAvailable” variable in the method. Then this method process the validation as shown in the image below………

So this callBackDalegateMethod will be called whenever the WebService will be ready with the response. So if web service returns the response in say 10 seconds, by the time, User can enter the password and so on.. And we can keep the “OK” button disable by that time as User can not click it before validation completes on “UserName” as shown in the image below…..

After this asynchronous validation, if UserName will not be available silverlight validation framework generates the error visualisation as shown in the image below……

 

So it was all about Asynchronous Data Validation with Silverlight 4.

For more info on silverlight, just go to, http://www.silverlight.net

For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/

For more details on Silverlight 4.0, you can also browse through following blogs………

http://timheuer.com/blog/          —  Tim Heuer Blog – Silverlight Program Manager

http://blogs.silverlight.net/blogs/jesseliberty/       — Jesse Liberty Blog – Silverlight Program Manager

http://johnpapa.net/              —  John Papa website

http://adamkinney.wordpress.com/          — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend

With silverlight 3 we were able to validate data using ‘INotifyPropertyChanged’  (System.ComponenetModel.INotifyPropertyChanged) interface.

Now in silverlight 4 we have one new interface called ‘INotifyDataErrorInfo’  (System.ComponenetModel.INotifyDataErrorInfo) as well.

 

Any Entity class can implement both or any of these interfaces to validate the data.

I am using the Entity class “User” to validate the data (Name and Email) as shown in the image below……

Here “Name” is validating through “INotifyDataErrorInfo” and “Email” is validating through “INotifyPropertyChanged”.

Validation using “INotifyPropertyChanged” :

To use “INotifyPropertyChanged”, we need to implement one event “PropertyChanged” in code behind file as shown in the image above (second image in the post).

We also need to set couple of attributes (“ValidatesOnExceptions”, “NotifyOnValidationError”) on the control in XAML code which actually performs the validation as shown in the image below……

Now when the value of property “email” is changed, code in the following image throws an exception…..

And this exception is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….

Validation using “INotifyDataErrorInfo” :

To use “INotifyDataErrorInfo” we need to implement one event “ErrorsChanged”, one method “GetErrors” and one property “HasErrors” in the code behind file as shown in the image above (second image in the current blog post).

Here we do not need to implement anything in XAML code to perform validation as in case of “INotifyPropertyChanged”.

Now when the value of property “name” is changed, code in the following image validates the value of “name” and called the event “ErrorsChanged” and which in turns called the method “GetErrors()” which returns the “errors” (or null if there is no error) as shown in the image below…….

And then if “GetError()” method returns any error is then handled by silverlight validation framework and it creates the error visualization on silverlight control as shown in the folowing image…….

Silverlight validation framework seems lot more sound with silverlight 4 now in compare with silverlight 3. Silverlight 4 validation framework also supports “Asynchronous Data Validation”, which I will discuss in my next silverlight blog post.

 For more info on silverlight, just go to, http://www.silverlight.net

For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/

For more details on Silverlight 4.0, you can also browse through following blogs………

http://timheuer.com/blog/          —  Tim Heuer Blog – Silverlight Program Manager

http://blogs.silverlight.net/blogs/jesseliberty/       — Jesse Liberty Blog – Silverlight Program Manager

http://johnpapa.net/              —  John Papa website

http://adamkinney.wordpress.com/          — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend