Effective Validation Techniques with MVVM in Silverlight

Play Effective Validation Techniques with MVVM in Silverlight

The Discussion

  • User profile image

    Great talk. Thank you.

  • User profile image

    Where can we find the examples code?

  • User profile image
    xiang zhang

    Nice talk.

  • User profile image

    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!

  • User profile image

    Very very good stuff!

    Hope to try it soon on my work.

  • User profile image
    Luis Abreu

    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).

  • User profile image
    Anders Rundqvist

    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?

  • User profile image

    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.

  • User profile image

    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 Smiley ). 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 Smiley

  • User profile image

    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…

  • User profile image
    Atif Shahzad

    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.


  • User profile image

    @Atif Shahzad:

    Hi Atif!

    You kind of copy pasted my answer here Smiley


    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 Smiley

    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!



  • User profile image

    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?



Add Your 2 Cents