Bv202 Bv202 - 1 month ago 5
C# Question

Disable save button when validation fails

As you can likely see from the title, I am about to ask something which has been asked many times before. But still, after reading all these other questions, I cannot find a decent solution to my problem.

I have a model class with basic validation:

partial class Player : IDataErrorInfo
{
public bool CanSave { get; set; }

public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Firstname")
{
if (String.IsNullOrWhiteSpace(Firstname))
{
result = "Geef een voornaam in";
}
}
if (columnName == "Lastname")
{
if (String.IsNullOrWhiteSpace(Lastname))
{
result = "Geef een familienaam in";
}
}
if (columnName == "Email")
{
try
{
MailAddress email = new MailAddress(Email);
}
catch (FormatException)
{
result = "Geef een geldig e-mailadres in";
}
}
if (columnName == "Birthdate")
{
if (Birthdate.Value.Date >= DateTime.Now.Date)
{
result = "Geef een geldige geboortedatum in";
}
}

CanSave = true; // this line is wrong
return result;
}
}

public string Error { get { throw new NotImplementedException();} }
}


This validation is done everytime the property changes (so everytime the user types a character in the textbox):

<TextBox Text="{Binding CurrentPlayer.Firstname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="137" IsEnabled="{Binding Editing}" Grid.Row="1"/>


This works perfect. The validation occurs (the
PropertyChanged
code for the binding is done in the VM on the CurrentPlayer property, which is an object of Player).

What I would like to do now is disable the save button when the validation fails.

First of all, the easiest solutions seems to be found in this thread:

Enable Disable save button during Validation using IDataErrorInfo


  1. If I want to follow the accepted solution, I'd have to write my
    validation code twice, as I cannot simply use the indexer. Writing
    double code is absolutely not what I want, so that's not a solution
    to my problem.

  2. The second answer on that thread sounded very promising as first,
    but the problem is that I have multiple fields that have to be
    validated. That way, everything relies on the last checked property
    (so if that field is filled in correctly,
    CanSave
    will be true, even
    though there are other fields which are still invalid).



One more solution I've found is using an
ErrorCount
property. But as I'm validating at each property change (and so at each typed character), this isn't possible too - how could I know when to increase/decrease the
ErrorCount
?

What would be the best way to solve this problem?

Thanks

Answer

I've implemented the map approach shown in my comment above, in C# this is called a Dictionary in which I am using anonymous methods to do the validation:

partial class Player : IDataErrorInfo
{
    private delegate string Validation(string value);
    private Dictionary<string, Validation> columnValidations;
    public List<string> Errors;

    public Player()
    {
        columnValidations = new Dictionary<string, Validation>();
        columnValidations["Firstname"] = delegate (string value) {
            return String.IsNullOrWhiteSpace(Firstname) ? "Geef een voornaam in" : null;
        }; // Add the others...

        errors = new List<string>();
    }

    public bool CanSave { get { return Errors.Count == 0; } }

    public string this[string columnName]
    {
        get { return this.GetProperty(columnName); } 

        set
        { 
            var error = columnValidations[columnName](value);

            if (String.IsNullOrWhiteSpace(error))
                errors.Add(error);
            else
                this.SetProperty(columnName, value);
        }
    }
}