none
How to Force IDataErrorInfo to re validate using EF & MVVM ?

    Question

  • I use EF4 (edmx) and MVVM.

    i use partial class to implement IDataErrorInfo on my EF entities.

    Problem : certain validation depends on some values got from user.

    Below an extract of my partial class validation :

     

     

    string ValidateMinimumAmount() {
    
      if (this.type_account != "INTERNAL")
        return null;
    
      if (this.account_minimum==null)
      {
        return "You must provide a minimum amount";
      }
      return null;
    }
    

    Since the type_account IS NOT "INTERNAL" at the begining, when the user sets it to "INTERNAL" via a combobox in myapp, it never re validate again !

    I'm trying to find a way to force WPF to re validate BUT:

    -in my ViewModel i can't call NotifyPropertyChanged on my entity because it is not part of my VM.

    Even when i try to call NotifyPropertyChanged on my VM SelectedIem the validation does not occur.

    Any help ??

    Thanks

    John

     

     

     


    Jonathan
    Thursday, March 24, 2011 11:29 AM

Answers

  • This is quite a complex issue. The problem is that you want to validate in your model, but represent the errors via the view model. What you should do in your view model is check for property changes. When a property on the view model changes, you should set it on the model to make it cause an error. Then, you will have to read the errors (via IDataErrorInfo) and set the same error on the view model.

    Sounds easy, but this is very complex. For example, what if the view model is canceled. Then, you will need to restore the original values on the model as well.

    In Catel, we have thought of this issue and handled ViewModelToModel mappings. This way, errors and values are directly passed from the view model to the model and back on changes. This way, you can still handle the validation in the model and pass it through to your view model (and eventually the view) via simple attributes.

    This article explains all about the attributes in Catel and how they should be used. You can also consider writing this yourself.


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a free open-source MVVM framework for WPF and Silverlight? Check out Catel!
    Thursday, March 24, 2011 2:04 PM