Alan Wayne
Alan Wayne

Reputation: 5394

How to show validatioin errors on different columns from IDataErrorInfo?

The Problem: I would like the default error of inclosing the input textbox on the UI to be used, but input in one field needs to highlight other multiple textboxes that have error.

Example: If all textboxes are empty, then no error. If only one textbox has a string, then both the OTHER two textbox's should be highlighted. (The textbox with the data is NOT the error, but the other two--if empty--are now in error). If the string is then removed and all textboxes are empty, then no error and nothing should be highlighted.

I can't seem to find an answer to this simple question. I have a business object (MVVM) with three properties, each of which is bound to a textbox in the XAML as:

  <wc:AutoFilteredComboBox 
              .. 
              Text="{Binding ReferredBy.NewReferralName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
 ....
/>


  <TextBox  
                   Text="{Binding ReferredBy.Phone, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
         />

 <TextBox 
     ......            
        Text="{Binding ReferredBy.PriorAuthorizationNumber, ValidatesOnDataErrors=True}" 
         />

And the ViewModel implementing IDataErrorInfo:

  public string Error
    {
        get { return null; }
    }

    // any returned non-empty string is an error.
    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "NewReferralName":

                    if (!String.IsNullOrWhiteSpace(PriorAuthorizationNumber) || !String.IsNullOrWhiteSpace(Phone))
                    {
                        if (String.IsNullOrWhiteSpace(NewReferralName))
                            return "NewReferralName is invalid";
                    }
                    break;
                case "Phone":
                    if (!String.IsNullOrWhiteSpace(NewReferralName) && String.IsNullOrWhiteSpace(Phone))
                    {
                            return "Phone is invalid";
                    }
                    break;
                case "PriorAuthorizationNumber":
                    if (!String.IsNullOrWhiteSpace(NewReferralName) && String.IsNullOrWhiteSpace(PriorAuthorizationNumber))
                    {
                        return "PriorAuthorizationNumber is invalid";
                    }
                    break;
            }
            // string.Empty is no error.
            return string.Empty;
        }
    }

So, if data is input to one textbox, how can errors be shown on the other textbox's? Can IDataErrorInfo be used for this, or is there a better way?

TIA

Upvotes: 0

Views: 288

Answers (1)

antiocol
antiocol

Reputation: 548

Of course you can use IDataErrorInfo to achieve this. The trick here is "telling" the view to reevaluate your validations with the aid of INotifyPropertyChanged. When user change the value of one property, then the ViewModel not only have to notify the View that property changed, but other involved ones too.

For example:

public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
{
     // Do this for each involved property in your ViewModel
     private string _newReferralName;
     public string NewReferralName
     {
         get { return _newReferralName; }
         set
         {
             _name = value;
             RaisePropertyChanged("NewReferralName");

             // The tricky part. Notify that the related properties 
             // have to be refreshed (in the View) and, thus, reevaluated
             RaisePropertyChanged("Phone");
             RaisePorpertyChanged("PriorAuthorizationNumber");
         }
     }
     ...

     // INotifyPropertyChanged implementation
     public event PropertyChangedEventHandler PropertyChanged;
     void RaisePropertyChanged(string prop)
     {
         if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
     }

}

Upvotes: 1

Related Questions