9

How to disable/enable a button while doing validation using IDataErrorInfo?

I am using MVVM using GalaSoft light Framework. In my Model class I have implemented IDataErrorInfo to display the error messages.

public string this[string columnName]
{
    get
    {
        Result = null;
        if (columnName == "FirstName")
        {
            if (String.IsNullOrEmpty(FirstName))
            {
                Result = "Please enter first name";
            }
        }
        else if (columnName == "LastName")
        {
            if (String.IsNullOrEmpty(LastName))
            {
                Result = "Please enter last name";
            }
        }

        else if (columnName == "Address")
        {
            if (String.IsNullOrEmpty(Address))
            {
                Result = "Please enter Address";
            }
        }

        else if (columnName == "City")
        {
            if (String.IsNullOrEmpty(City))
            {
                Result = "Please enter city";
            }
        }

        else if (columnName == "State")
        {
            if (State == "Select")
            {
                Result = "Please select state";
            }
        }

        else if (columnName == "Zip")
        {
            if (String.IsNullOrEmpty(Zip))
            {
                Result = "Please enter zip";

            }
            else if (Zip.Length < 6)
            {
                Result = "Zip's length has to be at least 6 digits!";

            }
            else
            {
                bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");

                if (zipNumber == false)
                {
                    Result = "Please enter only digits in zip";


                }
            }
        }
        else if (columnName == "IsValid")
        {
            Result = true.ToString();
        }

        return Result;

    }
}

Screenshot: https://i.stack.imgur.com/kwEI8.jpg

How to disable/enable save button. Kindly suggest?

Thanks

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Tarun
  • 221
  • 1
  • 6
  • 14

3 Answers3

18

The Josh Smith Way of doing this is to create the following methods in the Model:

static readonly string[] ValidatedProperties =
{
    "Foo",
    "Bar"
};

/// <summary>
/// Returns true if this object has no validation errors.
/// </summary>
public bool IsValid
{
    get
    {
        foreach (string property in ValidatedProperties)
        {

            if (GetValidationError(property) != null) // there is an error
                return false;
        }

        return true;
    }
}

private string GetValidationError(string propertyName)
{
    string error = null;

    switch (propertyName)
    {
        case "Foo":
            error = this.ValidateFoo();
            break;

        case "Bar":
            error = this.ValidateBar();
            break;

        default:
            error = null;
            throw new Exception("Unexpected property being validated on Service");
    }

    return error;
}

The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:

/// <summary>
/// Checks if all parameters on the Model are valid and ready to be saved
/// </summary>
protected bool CanSave
{
    get
    {
        return modelOfThisVM.IsValid;
    }
}

Finally, if you are using RelayCommand, you can set the predicate of the command to the CanSave property, and the View will automatically enable or disable the button. In the ViewModel:

/// <summary>
/// Saves changes Command
/// </summary>
public ICommand SaveCommand
{
    get
    {
        if (_saveCommand == null)
            _saveCommand = new RelayCommand(param => this.SaveChanges(), param => this.CanSave);

        return _saveCommand;
    }
}

And in the View:

<Button Content="Save" Command="{Binding Path=SaveCommand}"/>

And that's it!

PS: If you haven't read Josh Smith's article yet, it will change your life.

Pieter Müller
  • 4,573
  • 6
  • 38
  • 54
  • that's not a good approach to put the validation logic in your Model, because validation logic may change in different scenario. – Amir Oveisi May 02 '17 at 17:46
  • For simplicity it can be just `public bool IsValid => ValidatedProperties.All(p => GetValidationError(p) == null);`. – Daniel Dušek Nov 23 '17 at 21:04
  • my problem with such approach is that indeed it handle String errors , but not Decimal,long,double .. extra when the user enter *'ABCDE...'*, a red border show up around the cell in datagrid but there is no indication in C# that this error is happening , is there is any solution at all for this ? setting "price" to string is not a good option because it will require a lot of converting to multiply it to Quantity and TotalPrice , TAX .. etc this will ruin the app and make it even harder to manage ... – The Doctor Jul 03 '22 at 20:26
8

you can add add a boolean property CanSave and set it at the end of your valiation method. Bind the IsEnabled from your button to IsValid. Somthing like this:

public bool CanSave
{
  get{ return canSave; }
  set{ canSave = value; RaisePropertyChanged( "CanSave" ); }
}
private bool canSave;

public string this[string columnName]
{
  //....
  CanSave = Result == String.Empty;
}

//xaml
<Button IsEnabled={Binding Path=CanSave}>Save</Button>
stijn
  • 34,664
  • 13
  • 111
  • 163
  • This is actually much better than the accepted answer if you use `IDataErrorInfo` if you just use validation rule attributes like I am right now, you probably have to go with accepted answer :) – GONeale Jun 14 '12 at 06:01
  • 5
    Not working if there is more than one validation... this[string columnName] is called per property. So if a prop1 is invalid, and prop2 is valid, CanSave is set to true. – Peter van Kekem Jun 12 '13 at 08:07
  • @PetervanKekem that part is in the `//....`: everytime the function is called, `Result` has to be recalculated based on the previous result.. – stijn Jun 12 '13 at 09:23
  • Yes that's possible. However, I think it's too risky/complicated. I've chosen to implement @PieterMüller's solution. – Peter van Kekem Jun 12 '13 at 11:52
1

Here is my way of doing it using a combination of IDataErrorInfo interface, ValidationErrors Dictionary, and MVVM-Light messaging system. Straight forward and works like charm:

Model Class

public Dictionary<string, string> ValidationErrors = new Dictionary<string, string>();

public string this[string columnName]
{
  get
   {
     // Remove Property error from ValidationErrors prior to any validation
     ValidationErrors.Remove(propertyName);
     //----------------------------------------
     string Result = null;
     if (columnName == "FirstName")
     {
        if (String.IsNullOrEmpty(FirstName))
        {
           // Add Property error to ValidationErrors Dic
           ValidationErrors[propertyName] = Result = "Please enter first name";
           //----------------------------------------
        }
     }
     else if (columnName == "LastName")
     {
        if (String.IsNullOrEmpty(LastName))
        {
          // Add Property error to ValidationErrors Dic
          ValidationErrors[propertyName] = Result = "Please enter last name";
          //----------------------------------------
        }
     }

    // Send MVVM-Light message and receive it in the Code Behind or VM
    Messenger.Default.Send<PersonInfoMsg>(new PersonInfoMsg());
    //----------------------------------------
    return Result;
  }
}

View Code Behind

 public partial class PersonInfoView : UserControl
  {
    public PersonInfoView()
    {
        InitializeComponent();
        Messenger.Default.Register<PersonInfoMsg>(this, OnErrorMsg);
    }

    private void OnErrorMsg(PersonInfoMsg)
    {
        // In case of DataGrid validation
        foreach (PersonInfoModel p in GridName.ItemsSource)
        {
            if (p.ValidationErrors.Count == 0)
                SaveBtn.IsEnabled = true;
            else
                SaveBtn.IsEnabled = false;
        }
     }
  }
usefulBee
  • 9,250
  • 10
  • 51
  • 89
  • the problem i faced with such approach is that indeed it handle String errors , but not Decimal,long,double .. extra when the user enter *'ABCDE...'*, a red border show up around the cell in datagrid but there is no indication in C# that this error is happening , is there is any solution at all for this ? setting "price" to string is not a good option because it will require a lot of converting to multiply it to Quantity and TotalPrice , TAX .. etc this will ruin the app and make it even harder to manage ... – The Doctor Jul 03 '22 at 20:27