Is is possible to use a combination of ValidationRules and INotifyDataErrorInfo for wpf validation?
Asked Answered
J

1

8

In WPF there are 3 ways to do validation:

  • Validation Rules
  • IDataErrorInfo
  • INotifyDataErrorInfo

Is it possible to use a combination of these at the same time? For my needs, I would like to validate new rules with the flexibility of INotifyDataErrorInfo, but don't want to interfere with existing ValidationRules for the same object I want to validate.

Janae answered 10/11, 2015 at 12:1 Comment(0)
W
4

1. Mixing the different source of errors

Yes you can mix the three kind of Validation you quote :

  • ValidationRules are fine for GUI(surface) validation
  • IDataErrorInfo is implemented on the view model/business object. It is fine for more business oriented validation
  • INotifyDataErrorInfo is also implemented on the view model/business object. It adds possibility of multiple errors on a given field and also adds asynchronous validations (i.e. a server or a thread can take time to answer to the valildation).

That last source of error is really more longer to implement

2. Taking errors into account

The most difficult is to take into account the different sources of error when you want to prevent a window from being closed if data is not valid.

ValidationRules error must be looked up in the GUI bindings, because the invalid data doesn't get to the business object/ViewModel.

IDataErrorInfo and INotifiDataErrorInfo can be looked in the ViewModel layer.

Regards

Waldgrave answered 10/11, 2015 at 13:14 Comment(3)
I used the combination of the two and I run into some side effects that I can tolerate for now.Janae
Of course it can be tolerable, and even useful ;-). Because these aren't the same kind of validations, they can be complementary.Waldgrave
@Janae what were the side effects you were talking about? I'm about to implement some validation rules along existing INotifyDataErrorInfor implementation as I don't want user to add specific characters into my model. But if they are side effects it would be interesting to know and that will make the answer more informative.Mouldy

© 2022 - 2024 — McMap. All rights reserved.