Validate a property

Jul 26, 2007 at 9:05 AM
Hi

I don't realy understand how to use this framework

What I need is to test if a property is valid or not

What I've done is create a entity class that take the queryString in parameter and each property get one value of it.

So know I need something like this

if( Aclass.Validate(myClass.MyProperty))

Aclass is a class of the validation framework

myClass is my entity object


Thank
Aug 2, 2007 at 2:12 AM
There are a number of options for achieving what you want.

Here is one way
using System.Collections.Generic;
using ValidationFramework;
 
namespace ExampleLibraryCSharp
{
    public class PersonExample : DataErrorInfoValidatableBase
    {
 
        #region Fields
 
        //Define run-time constants here so UI code has compile-time
        //checks against misspellings in data binding expressions.
        public const string FirstNameMember = "FirstName";
        public const string LastNameMember = "LastName";
        public const string EmailAddressMember = "EmailAddress";
        public const string PhoneNumberMember = "PhoneNumber";
        private string firstName;
        private string lastName;
        private string phoneNumber;
        private string emailAddress;
 
 
        #endregion
 
        #region Constructor
 
        public PersonExample()
            : base(true)
        {
        }
 
        #endregion
 
        #region Properties
 
        [RequiredStringRule]
        [LengthStringRule(50)]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if (firstName != value)
                {
                    firstName = value;
                    ValidateProperty(FirstNameMember);
                }
            }
        }
 
        [RequiredStringRule]
        [LengthStringRule(50, Minimum = 2)]
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                if (lastName != value)
                {
                    lastName = value;
                    ValidateProperty(LastNameMember);
                }
            }
        }
 
        [RequiredStringRule]
        [RegexRule(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
          ErrorMessage = "Invalid email format.")]
        [LengthStringRule(50)]
        public string EmailAddress
        {
            get
            {
                return emailAddress;
            }
            set
            {
                if (emailAddress != value)
                {
                    emailAddress = value;
                    ValidateProperty(EmailAddressMember);
                }
            }
        }
 
        [RegexRule(@"\d{2}-\d{4}-\d{4}",
          ErrorMessage = "Phone number must be of the format 11-1111-1111")]
        public string PhoneNumber
        {
            get
            {
                return phoneNumber;
            }
            set
            {
                if (phoneNumber != value)
                {
                    phoneNumber = value;
                    ValidateProperty(PhoneNumberMember);
                }
            }
        }
        #endregion
    }
 
    public class TestHarness
    {
        public void Run()
        {
            PersonExample personExample = new PersonExample();
            string firstNameErrorMessage = personExample[PersonExample.FirstNameMember];
            bool isInstanceValid = personExample.IsValid;
            IList<ValidationResult> allValidationResultsInError = personExample.ValidatorResultsInError;
        }
    }
}


For a better idea I recommend you download the latest source http://www.codeplex.com/ValidationFramework/SourceControl/ListDownloadableCommits.aspx and run some of the quick starts.