|
namespace System.Web.UI
{
using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
/// <summary>
/// Provides validation information of the model instance.
/// </summary>
/// <typeparam name="T">Type of model object.</typeparam>
public class ModelStateValidator<T> : Dictionary<String, String>
{
T model;
RequiredAttribute required;
RangeAttribute range;
RegularExpressionAttribute regex;
StringLengthAttribute stringlngt;
/// <summary>
/// Creates a new instance of the current System.Object
/// </summary>
/// <param name="model">Model instace to validate.</param>
public ModelStateValidator(T model)
{
this.model = model;
}
/// <summary>
/// Indicating whether this instance of the model is valid asper validation rules applied.
/// </summary>
public virtual bool IsValid
{
get
{
bool isValid = true;
foreach (PropertyInfo property in typeof(T).GetProperties())
{
//// If RequiredAttribute is defined, checks whether the property is valid of not.
if (property.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0)
{
required = (RequiredAttribute)property.GetCustomAttributes(typeof(RequiredAttribute), false)[0];
if (!required.IsValid(property.GetValue(model, null)))
{
isValid = false;
this.Add(property.Name, String.IsNullOrEmpty(required.ErrorMessage) ? property.Name + " is required." : required.ErrorMessage);
}
}
//// If RangeAttribute is defined, checks whether the property is valid of not.
else if (property.GetCustomAttributes(typeof(RangeAttribute), false).Length > 0)
{
range = (RangeAttribute)property.GetCustomAttributes(typeof(RangeAttribute), false)[0];
if (!range.IsValid(property.GetValue(model, null)))
{
isValid = false;
this.Add(property.Name, String.IsNullOrEmpty(range.ErrorMessage) ? property.Name + " is invalid." : range.ErrorMessage);
}
}
//// If RegularExpressionAttribute is defined, checks whether the property is valid of not.
else if (property.GetCustomAttributes(typeof(RegularExpressionAttribute), false).Length > 0)
{
regex = (RegularExpressionAttribute)property.GetCustomAttributes(typeof(RegularExpressionAttribute), false)[0];
if (!regex.IsValid(property.GetValue(model, null)))
{
isValid = false;
this.Add(property.Name, String.IsNullOrEmpty(regex.ErrorMessage) ? property.Name + " is invalid." : regex.ErrorMessage);
}
}
//// If StringLengthAttribute is defined, checks whether the property is valid of not.
else if (property.GetCustomAttributes(typeof(StringLengthAttribute), false).Length > 0)
{
stringlngt = (StringLengthAttribute)property.GetCustomAttributes(typeof(StringLengthAttribute), false)[0];
if (!stringlngt.IsValid(property.GetValue(model, null)))
{
isValid = false;
this.Add(property.Name, String.IsNullOrEmpty(stringlngt.ErrorMessage) ? property.Name + " exceeds maximum length." : stringlngt.ErrorMessage);
}
}
}
return isValid;
}
}
}
}
|
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.
I work as a Technology Specialist in Cognizant Technology Solutions. I have 1.5 decades of experience in Software Development and focuses on Microsoft Web Technologies, JavaScript Frameworks, Azure Services and DevOps