3

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

Community
  • 1
  • 1
Bv202
  • 3,924
  • 13
  • 46
  • 80
  • Avoid writing all your validation classes like this, this welcomes quite some factoring. Notable is how the column names map to if blocks with error results, so a map to delegate functions can be used for them; yielding all the `if`s unnecessary. But let's suppose you were to use them, you're doing an `if (B)` in an `if (A)` so you could do an `if (A && B)` instead. You don't have to type `{` and `}` characters if you only execute one statement in the `if`. By using the map, this example can be on ~20 lines instead of ~50 lines; speeding up your development by double. – Tamara Wijsman Nov 14 '12 at 21:12

3 Answers3

3

This article http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validating-with-the-idataerrorinfo-interface-cs moves the individual validation into the properties:

public partial class Player : IDataErrorInfo
{
    Dictionary<string, string> _errorInfo;

    public Player()
    {
        _errorInfo = new Dictionary<string, string>();
    }

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

    public string this[string columnName]
    {
        get 
        { 
            return _errorInfo.ContainsKey(columnName) ? _errorInfo[columnName] : null;
        }
    }

    public string FirstName
    {
        get { return _firstName;}
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                _errorInfo.AddOrUpdate("FirstName", "Geef een voornaam in");
            else
            {
                _errorInfo.Remove("FirstName");
                _firstName = value;
            }
        }
    }
}

(you would have to handle the Dictionary AddOrUpdate extension method). This is similar to your error count idea.

Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
jimbojones
  • 682
  • 7
  • 19
  • Edited: `_firstName` should be set in the else clause, else you can set the object to an invalid state. This also removes an occurrence of `FirstName` which makes it handier to create extra properties as less parts of the code need to be replaced. – Tamara Wijsman Nov 14 '12 at 21:17
  • Thanks, this seems like a nice solution. The problem with it is that my properties are defined in the other partial class (generated code) and these are automatic properties. Can this solution be applied somehow? – Bv202 Nov 14 '12 at 21:22
1

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);
        }
    }
}
Community
  • 1
  • 1
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • Thanks, but I'm having the same problem as in the solution below... the constructor is in the other part of the class, which is generated and so I cannot write my own constructor... – Bv202 Nov 14 '12 at 21:42
  • Lazy load the column validations in the setter public string this[string columnName] { get { return this[columnName]; } set { if ( columnValidations == null ) { LoadColumnValidations(); } var error = columnValidations[columnName](value); if (String.IsNullOrWhiteSpace(error)) errors.Add(error); else this[columnName] = value; } } – jimbojones Nov 14 '12 at 21:50
  • Hmm thanks, that would be an option. But to be honest, I don't really understand the example. What exactly does the get section of the indexer return? So what will return this[columnName]; actually return? – Bv202 Nov 14 '12 at 21:55
  • But what exactly does this[columnName] refer to? – Bv202 Nov 14 '12 at 22:33
  • Your getter will throw a stackoverflowexception. –  Nov 15 '12 at 14:22
  • 1
    @Will: Meant to use `getProperty` and `setProperty` there to bypass the operator. – Tamara Wijsman Nov 15 '12 at 17:06
  • @Bv202: See my previous comment, corrected that in the above example. – Tamara Wijsman Nov 16 '12 at 20:37
0

This approach works with Data Annotations. You can also bind the "IsValid" property to a Save button to enable/disable.

public abstract class ObservableBase : INotifyPropertyChanged, IDataErrorInfo
{
    #region Members
    private readonly Dictionary<string, string> errors = new Dictionary<string, string>();
    #endregion

    #region Events

    /// <summary>
    /// Property Changed Event
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Protected Methods

    /// <summary>
    /// Get the string name for the property
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="expression"></param>
    /// <returns></returns>
    protected string GetPropertyName<T>(Expression<Func<T>> expression)
    {
        var memberExpression = (MemberExpression) expression.Body;
        return memberExpression.Member.Name;
    }

    /// <summary>
    /// Notify Property Changed (Shorted method name)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="expression"></param>
    protected virtual void Notify<T>(Expression<Func<T>> expression)
    {
        string propertyName = this.GetPropertyName(expression);
        PropertyChangedEventHandler handler = this.PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    /// <summary>
    /// Called when [property changed].
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="expression">The expression.</param>
    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> expression)
    {
        string propertyName = this.GetPropertyName(expression);
        PropertyChangedEventHandler handler = this.PropertyChanged;

        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets an error message indicating what is wrong with this object.
    /// </summary>
    public string Error => null;

    /// <summary>
    /// Returns true if ... is valid.
    /// </summary>
    /// <value>
    ///   <c>true</c> if this instance is valid; otherwise, <c>false</c>.
    /// </value>
    public bool IsValid => this.errors.Count == 0;

    #endregion

    #region Indexer

    /// <summary>
    /// Gets the <see cref="System.String"/> with the specified column name.
    /// </summary>
    /// <value>
    /// The <see cref="System.String"/>.
    /// </value>
    /// <param name="columnName">Name of the column.</param>
    /// <returns></returns>
    public string this[string columnName]
    {
        get
        {
            var validationResults = new List<ValidationResult>();
            string error = null;

            if (Validator.TryValidateProperty(GetType().GetProperty(columnName).GetValue(this), new ValidationContext(this) { MemberName = columnName }, validationResults))
            {
                this.errors.Remove(columnName);
            }
            else
            {
                error = validationResults.First().ErrorMessage;

                if (this.errors.ContainsKey(columnName))
                {
                    this.errors[columnName] = error;
                }
                else
                {
                    this.errors.Add(columnName, error);
                }
            }

            this.OnPropertyChanged(() => this.IsValid);
            return error;
        }
    }

    #endregion  
}
LawMan
  • 3,469
  • 1
  • 29
  • 32