This project is read-only.
Cross-Field Validation

The following example show how it is possible to validate a field based on another field value.

    class Program
    {
        public class NewUserRegistration
        {
            public string UserName { get; set; }
            public string Password { get; set; }
            public string ConfirmPassword { get; set; }
        }

        static void Main(string[] args)
        {
            Engine engine = new Engine();
            engine.For<NewUserRegistration>()
                .Setup(user => user.UserName)
                    .MustNotBeNullOrEmpty()
                .Setup(user => user.Password)
                    .MustNotBeNullOrEmpty()
                .Setup(user => user.ConfirmPassword)
                    .MustEqual(user => user.Password);

            var user1 = new NewUserRegistration() { UserName = "user1", Password = "123", ConfirmPassword = "123"};
            var user2 = new NewUserRegistration() { UserName = "user2", Password = "123", ConfirmPassword = "456" };

            //User1 is valid
            var user1Valid = engine.Validate(user1);

            //User2 is not valid - ConfirmPassword and Password do not match
            var user2Valid = engine.Validate(user2);
        }
    }

Last edited Sep 6, 2011 at 10:51 AM by athoma13, version 2

Comments

No comments yet.