Consider this form model:

        public class NewUserForm
        {
                public string Password { get; set; }
                public string PasswordRepeated { get; set; }
        }

I am using NHibernate Validator, which easily lets me validate that
the password is set, like so:

        public class NewUserForm
        {
                [NotEmpty]
                public string Password { get; set; }
                public string PasswordRepeated { get; set; }
        }

Then I wanted to validate that the password was properly repeated. My
first attempt went like this:

        public class NewUserForm
        {
                [NotEmpty]
                public string Password { get; set; }

                [SameAs("Password")]
                public string PasswordRepeated { get; set; }
        }

That is, by implementing my own validation attribute with a
corresponding `IInitializableValidator` - but then I have access to
only the value of `PasswordRepeated` in the `IsValid` method, which
makes it impossible to compare the passwords.

Then I made my own validator at the class level, like this:

        [ValidateClass(typeof(PasswordMustBeRepeated))]
        public class NewUserForm
        {
                [NotEmpty]
                public string Password { get; set; }

                public string PasswordRepeated { get; set; }
        }

- implementing the necessary logic in `PasswordMustBeRepeated` which
would be called by my custom class-level validation attribute. That
worked, but then there doesn't seem to be a way to give back
information on _which_ property caused an error in the event that the
validation fails.

Has anyone got any suggestions on how I can validate that
`PasswordRepeated` is equal to `Password` and be able to know
afterwards that it was during the validation of `PasswordRepeated`
that the validation failed?

Perhaps I could add a special `IRuleArgsWithPropertyName` to allow for
specifying the name of a property whose name should be reported as the
name of the property for which the validation failed? That would allow
me to implement the special validation like this:

        public class NewUserForm
        {
                [NotEmpty]
                public string Password { get; set; }

                public string PasswordRepeated { get; set; }

                [AssertTrue(PropertyName = "PasswordRepeated")]
                public bool PasswordIsProperlyRepeated
                {
                    get { return Password == PasswordRepeated; }
                }
        }

Any suggestions?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NHibernate Contrib - Development Group" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com.ar/group/nhcdevs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to