Nested validation not updating on child VM change

179 views Asked by At

I have a scenario with some nested model which I want to do validation on.

Since my UI is implementing drag and drop behavior with MVVM, and I don't want to put that logic in the model, there is a VM for some of my model classes.

For example SiteConfiguration has a VM named SiteConfigurationViewModel.

In such case, SiteConfigurationViewModel has a property of type SiteConfiguration that exposes the model.

The root VM contains a list of the top level VMs and the view (DataWindow) has some binding to show the list (ListView) of VMs instances and a selected instance which the user can edit.

I am trying to implement validation but when I change the values in a nested VM/Model, the top window errors do not updated.

I tried many variations (e.g. validation annotations, overriding ValidateFields and ValidateBusinessRules, ViewModelToModel attribute) but I guess I am doing something wrong.

1

There are 1 answers

6
Geert van Horrik On

If you only add errors to the model (and not the view model), the errors won't show up automatically. The reason for this is that Catel adds a WarningAndErrorValidator control for the view model for you, not for the model (because you might want to hide some validation). Note that the vm is fully responsible for the validation.

If you use ViewModelToModel mappings, Catel will automatically map all the validation for you as well (so you can validate on the model, map it on the vm and the vm will automatically expose all the mapped warnings / errors).