|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Cobalt.Validation.Validators
{
/// <summary>
/// Performs validation based on a value not being null or empty.
/// </summary>
public class NotEmptyValidator : Validator
{
#region Protected Properties
/// <summary>
/// Gets the valid property types.
/// </summary>
/// <value>The valid property types.</value>
protected override Type[] ValidPropertyTypes
{
get { return new Type[] { typeof(IEnumerable) }; }
}
#endregion
#region Constructor(s)
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="propertyInfo">The property info.</param>
public NotEmptyValidator(PropertyInfo propertyInfo)
: this(null, propertyInfo)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="errorMessage">The error message.</param>
/// <param name="propertyInfo">The property info.</param>
public NotEmptyValidator(string errorMessage, PropertyInfo propertyInfo)
: base(errorMessage, propertyInfo)
{
if (string.IsNullOrEmpty(errorMessage))
this.ErrorMessage = string.Format(Resources.DefaultErrorMessages.NotEmpty, propertyInfo.Name);
}
#endregion
#region Protected Methods
/// <summary>
/// Does the validation.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
protected override bool DoIsValid(object instance, object value)
{
if(value == null)
return false;
IEnumerable ienum = value as IEnumerable;
if (ienum != null)
return ienum.GetEnumerator().MoveNext();
return false;
}
#endregion
#region Public Static Methods
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="propertyName">The name of the property.</param>
/// <returns></returns>
public static NotEmptyValidator CreateValidator<T>(string propertyName)
{
return CreateValidator(typeof(T), propertyName);
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns></returns>
public static NotEmptyValidator CreateValidator(Type type, string propertyName)
{
return new NotEmptyValidator(Validator.GetPropertyInfo(type, propertyName));
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="errorMessage">The error message.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns></returns>
public static NotEmptyValidator CreateValidator<T>(string errorMessage, string propertyName)
{
return CreateValidator(typeof(T), errorMessage, propertyName);
}
/// <summary>
/// Initializes a new instance of the <see cref="NotEmptyValidator"/> class.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="errorMessage">The error message.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns></returns>
public static NotEmptyValidator CreateValidator(Type type, string errorMessage, string propertyName)
{
return new NotEmptyValidator(errorMessage, Validator.GetPropertyInfo(type, propertyName));
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
I work at a financial services firm using mostly .NET. I enjoy my job immensely and love teaching, training, and mentoring younger developers.