I implemented a IDataErrorInfo interface in my class to validate the objects. But what I want to do now, is to validate a whole object programatically to know if it´s valid or not. I have been searching in google but can´t find anything. I thought I could do something like a object.Validate() and know if its valid, but IDataErrorInfo does not provides any method like that.
I already validate it using XAML, but I need to check it in a method too.
Can anybody help me to achieve this? Just in case, this is my class:
namespace Plutus.Data.Domain
{
using Plutus.Data.Domain.NameMappings;
using Plutus.Data.Domain.Validation;
using System;
using System.ComponentModel;
public class Phone : IDataErrorInfo
{
/// <summary>
/// Código de área.
/// </summary>
public string AreaCode { get; set; }
/// <summary>
/// Identificador de tipo de teléfono.
/// </summary>
public short? PhoneTypeID { get; set; }
/// <summary>
/// Número de teléfono.
/// </summary>
public long? PhoneNumber { get; set; }
/// <summary>
/// Número de interno.
/// </summary>
public string ExtensionNumber { get; set; }
public string Error { get { return null; } }
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "AreaCode")
{
if (string.IsNullOrWhiteSpace(AreaCode))
result = ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(columnName));
}
if (columnName == "PhoneTypeID")
{
if (!PhoneTypeID.HasValue)
result = ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(columnName));
}
if (columnName == "PhoneNumber")
{
if (!PhoneNumber.HasValue)
result = ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(columnName));
}
return result;
}
}
}
}
EXTENDED SOLUTION:
Maybe I didnt explain very good, but what I needed was to know if an object has any validation error or not. Based on ethicallogics solution, I created a new method called IsValid, where I check if there is any error on the dictionary:
public bool IsValid()
{
ValidateProperty("AreaCode");
ValidateProperty("PhoneNumber");
ValidateProperty("PhoneTypeID");
return Errors.Count == 0 ? true : false;
}
To implement this, I had to change the ValidateProperty method so as not to add again the error key in the dictionary (or you will get an exception). Then I checked first if the error is already in the dictionary, and I add it only if it doesnt:
public void ValidateProperty(string propertyName)
{
if (propertyName == "AreaCode" && string.IsNullOrWhiteSpace(AreaCode))
{
if (!Errors.ContainsKey(propertyName))
Errors.Add(propertyName, ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(propertyName)));
}
else if (propertyName == "PhoneTypeID" && !PhoneTypeID.HasValue)
{
if (!Errors.ContainsKey(propertyName))
Errors.Add(propertyName, ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(propertyName)));
}
else if (propertyName == "PhoneNumber" && !PhoneNumber.HasValue)
{
if (!Errors.ContainsKey(propertyName))
Errors.Add(propertyName, ErrorMessages.ErrorMessage(ErrorMessages.FieldIsRequired, PhoneNameDictionary.GetValue(propertyName)));
}
else if (Errors.ContainsKey(propertyName))
Errors.Remove(propertyName);
}
First your class must implement INotifyPropertyChanged
in xaml binding ValidateOnDataErrors must be True
Here I have returned simple string instead of your ErrorMessage because I dont have idea what it is , you can simply replace string with your ErrorMessage. I hope this will give you an idea.