Disable save button when validation fails

前端 未结 3 605
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 08:54

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

3条回答
  •  感情败类
    2021-01-14 09:37

    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 columnValidations;
        public List Errors;
    
        public Player()
        {
            columnValidations = new Dictionary();
            columnValidations["Firstname"] = delegate (string value) {
                return String.IsNullOrWhiteSpace(Firstname) ? "Geef een voornaam in" : null;
            }; // Add the others...
    
            errors = new List();
        }
    
        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);
            }
        }
    }
    

提交回复
热议问题