Effective Validation Techniques with MVVM in Silverlight
- Date: April 13, 2011 from 5:00PM to 6:00PM
- Day 2
- OPN05
- Speakers: Zoltan Arvai
- 30,830 Views
- 13 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Slides (view online)All input is evil! Protect your application and provide great user experience in your Silverlight application. Learn about the synchronous and asynchronous validation techniques Silverlight 4.0 offers and see how to implement these techniques in the most effective way with MVVM. Customize your validation feedback with Expression Blend for the maximum experience.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Great talk. Thank you.
Where can we find the examples code?
Thanks!
Nice talk.
www.zoltanarvai.com -> this is my blog... You can find the source fcode here! Thank you for your feedback, i'm glad you guys liked it!
Very very good stuff!
Hope to try it soon on my work.
Great presentation Zoltan. Just one small tiny thing I believe you didn't mention: how to integrate server side validation during a server side operation. Yes, you did show async validation, but the truth is that during that validation and the actual server side handling, the submitted data might be invalid. With your code base, it shouldn't be too hard to do that (and even to adapt it to present eventual exceptions thrown on the server side).
Great video! Zoltan, I think the data for the UI form should be stored in a Model and not directly in the ViewModel. The model instead inherit a ValidatingModelBase that contains the validation base code and the ViewModel is responsible for exposing a property containing that Model that the UI can get access to through data bindings. That way the model and validation rules can be reused everywhere in the application no matter where the data (Model) is used. Any comment about this?
Hi Luis!
Thanks, I'm happy you liked it!
Yeah, well you could definitely wire it up to handle these kind of scenarios as well. You just have to find a way to indicite the error back to the client and call AddError, and there you go... the infrastructure is already there. However I wouldn't mix exceptions into validation mechanism... that's something different semantically for me... But you could definitely do that.
Hi Anders,
After the talk we had a discussion on it I you can definitely change this architecture to be used by the model, and wire it up to the viewmodel... I even mentioned a way, where you just have to override the ValidateProperty method and modify it to use the validator on the model itself and not on the viewmodel... i think this one is the easyiest way.... Your approach works as well perfectly.
Anyway I agree with you, data should be stored in the model, but it's kind of up to preference (since you can statically codegen your viewmodels
). You have one thing to make sure, the viewmodel has to know, if there is something worng with the model... if you can stick to that, you are good to go 
I’m happy you like the session. So you want your entities to validate on the client side using RIA Services? Well, you have property validation by default, since RIA entities use attributes on properties and on classes to perform validation. So if you expose these entities on the UI, datagrid and dataform should be okay with it… Well if you want to expose only your mvvm model, but use the attributes over the ria entity properties, well then you should override the validatateproperty method in the viewmodel, (the one i created) and modified the code just a little bit, to perform validation on the selected properties of the selected mvvm properties, instead of the mvvm properties themselves… also you could automate this using a custom attribute over these properties… Hope this hepls… if you need more help, post a sample scenario, what and how you want it to work, and i’ll show you a sample code or a sample architecture…
Zoltan i did my property level validation inside my Domainservice Metadata class and its all goddd but what i want that when user save any data from silverlight client so MVVM first check the validation error on client and then if all fine then submitchanges to server this is what actually i want , what i do is to catch the submit operation then i got all the validation errors but i dont want to submit if is there any validation error in entity so Zoltan this is the stuff i want i hope you understand my problem and i hope you got the solution.
Regards
Ati
@Atif Shahzad:
Hi Atif!
You kind of copy pasted my answer here
Anyway:
I was curious about the client side... You use a DomainDataSource class in you xaml or a DomainContext in mvvm?
If your are using DDS than don't do that
If not...
You have a Submit method in your viewmodel, which calls SubmitChanges... In this method before you call submitchanges call your validation logic.
Now about the validation logic... on the client side you don't have metadata classes... that exists only on the server side... on the client side the attributes are placed over the properties of the original entitiy.
Now you have the validateproperty method in the viewmodel. Override it... See the code I posted on my blog for the session and rewrite that part where I use Validator.TryValidateProperty(this.....) instead of "this" you should add the ria entitiy you want to validate...
That's it... I hope it's clear!
Regards,
Zoltan
Hi Zoltan,
Thanks for your brillant presentation.
I have a question regarding property validation.
How do I make a button enabled on the same form only when all properties have been validated?
Do I need to check each property for errors? how? or is there a better way of doing it?
Can you please provide some sample code?
Cheers
C
Remove this comment
Remove this thread
close