Click here to Skip to main content
15,893,644 members
Articles / Web Development / ASP.NET

An Object Level Validation Framework

Rate me:
Please Sign up or sign in to vote.
4.94/5 (28 votes)
10 Nov 20065 min read 111.1K   492   85  
Provides a basic/advanced object level framework for validation.
using System;
using System.Collections.Generic;
using System.Reflection;

using Cobalt.Validation.Validators;

namespace Cobalt.Validation
{
    /// <summary>
    /// Implements a <see cref="LengthValidator"/> for the property.
    /// </summary>
    public class LengthValidatorAttribute : ValidatorAttribute
    {

        #region Private Fields

        private uint maxLength;
        private uint minLength;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the maximum length.
        /// </summary>
        /// <value>The maximum length.</value>
        public uint MaxLength
        {
            get { return this.maxLength; }
        }

        /// <summary>
        /// Gets the minimum length.
        /// </summary>
        /// <value>The minimum length</value>
        public uint MinLength
        {
            get { return this.minLength; }
        }

        #endregion

        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="LengthValidatorAttribute"/> class.
        /// </summary>
        /// <param name="maxLength">The maximum length.</param>
        public LengthValidatorAttribute(uint maxLength)
            : this(uint.MinValue, maxLength)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LengthValidatorAttribute"/> class.
        /// </summary>
        /// <param name="minLength">The minimum length..</param>
        /// <param name="maxLength">The maximum length.</param>
        public LengthValidatorAttribute(uint minLength, uint maxLength)
        {
            this.maxLength = maxLength;
            this.minLength = minLength;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the validator.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <returns></returns>
        public override Validator GetValidator(PropertyInfo propertyInfo)
        {
            return new LengthValidator(this.ErrorMessage, propertyInfo, this.minLength, this.maxLength);
        }

        #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.

License

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


Written By
Web Developer
United States United States
I work at a financial services firm using mostly .NET. I enjoy my job immensely and love teaching, training, and mentoring younger developers.

Comments and Discussions