Silverlight MVVM, RIA, and Validation
One of the biggest challenges of implementing validation in Silverlight is finding the appropriate technique based on your architecture and goals. There are lots of choices and lots of examples, but not all examples work in all scenarios. Plus many examples demonstrate techniques that are no longer necessary or valid in the current version of Silverlight or when using WCF RIA services.
Below are a set of posts on techniques for performing validation with Silverlight 4.0 using MVVM, WCF RIA Services, and custom business objects.
A few things to keep in mind if you are using Silverlight and WCF RIA Services:
- In most cases, the code should not need to throw exceptions for validation errors.
- The latest version of Silverlight/RIA provides automatic validation and a built-in validation error dictionary, so you do not have to write any code to build or populate validation errors.
The last point is an important one because there are videos and posts that go through the pages and pages of code required to manage your own validation and validation error dictionary. While this code is necessary and useful if you are using WCF directly, it is not necessary if you are using WCF RIA Services.
Here are the links:
- Setting up the sample project
- Simple single property validation using attributes
- Custom validation using attributes (for cross-property validation or complex single property validation)
- Custom validation using attributes (for full business object validation)
Click on a link to navigate to the desired post.
Enjoy!
Olivier — May 1, 2011 @ 10:28 pm
One big problem with validations is how to validate “last” field the user is always within while she click the “ok” button of the dialog.. Depending on scenarios, the “last” field does not lost focus and then is not validated.
Playing a bit with all this validation stuff and trying to implement it in real application let me see there are some problems of this kind..
Have you some advice to give ?
thanks in advanve (for the posts, and perhaps for your answer 🙂 ).
DeborahK — May 2, 2011 @ 12:05 am
Hi Oliver –
Using the sample application along with the changes to support single field validation, you should find that the data is also automatically validated before the submit. So the “last” field issue should not be a problem.
Try the sample code, adding in validation attributes as defined in this link: http://msmvps.com/blogs/deborahk/archive/2011/05/01/silverlight-wcf-ria-single-property-validation.aspx
And see if it is still a problem for you.
Hope this helps.
SUZIE — July 9, 2011 @ 5:06 am
Bravo very beautiful blog ! Thank’s
suzie — July 9, 2011 @ 5:13 am
Bravo very beautiful blog ! Thank’s
Ashwin — November 16, 2011 @ 7:53 pm
Is there any way to also addin async validation in ViewModel? i.e. to check if the entered email address already exists in the database?
John — December 5, 2012 @ 3:49 pm
Is VB code coming soon…?