Silverlight 4 Conditional Validation
I need to be able to validate some controls and I'm not sure how is the best way to do this. Some of the validation should be all the time like verifying string length on certain properties. This seemed to be easy enough.
I need to be able to check for required fields, but this app should allow for partial saving, so I need to be able to check for some condition (for instance a checkbox on the screen) and if that condition is met, then validate required, but allow saving if the checkbox is unchecked.
I have implement this via a custom validator and everything works fine client side, but once I try to save, the validation is checked on the server side and it throws an exception.
I'm usin开发者_如何学Pythong EF w/ data annotations on the RIA metadata to tag the properties with the validators.
If anyone can point me in the right direction on how to accomplish this I would appreciate it.
If you do your validations in your viewmodel on the properties setter, you can conditionally just throw exceptions, if your xaml is bound w/ ValidatesOnExceptions=True you should get the behavior you want.
It seams that you implemented the CustomValidation on the client side.
You should create this CustomValidation class on server side in a ".shared.cs" file, so that the customvalidation is available both on server and client. RIA will replicate this validator onto the client.
Be sure to have a property on your model class (maybe an additionnal property?) and bind your checkBox to this property. The server will know witch kind of validation will be used.
And finally use your customvalidation on the RIA metadata class on the server side ! again, RIA will replicate this validation onto the client.
精彩评论