Hello,

I am using NHibernate.Validator 1.2.0.2001 with the 2.1GA of
Nhibernate.  I am creating a custom class level validator and related
attribute which I am using to validate some logic before allowing the
object to be persisted.  I want to update the default error message
with details found during this validation but can't seem to figure out
how to dynamically update the message property.  Below is what I have
at the moment.  Any help would be greatly appreciated.


        [AttributeUsage(AttributeTargets.Class)]
        [ValidatorClass(typeof(SectionInstanceValidator))]
        public class SectionInstanceAttribute : Attribute, IRuleArgs
        {
            private string message = string.Empty;
            public string Message
            {
                get { return message; }
                set { message = value; }
            }
        }

        public class SectionInstanceValidator : IValidator
        {
            public bool IsValid(object value,
IConstraintValidatorContext constraintValidatorContext)
            {
                if (value is Section)
                {
                    string message = String.Empty;
                    var section = value as Section;
                    var numbers = section.SectionNumbers;
                    var formats =
Globals.BLL.Generic.GetAll<MasterSpecFormat>();

                    //Verify all MasterSpecFormats have been defined
                    foreach (var item in formats)
                    {
                        if (!numbers.Select(o =>
o.MasterSpecFormat).Contains(item))
                        {
                            if (message ==
String.Empty)
                                message += item.Name;
                            else
                                message += ", " + item.Name;
                        }
                    }
                    if (!String.IsNullOrEmpty(message))
                    {
                        //This is the dynamic message I want to return
with the validation error.
                        message = "Section number missing for " +
message;
                        return false;
                    }
                    else
                        return true;
                }
                else
                    return false;
            }
        }


Thanks,
Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" 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/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to