views:

34

answers:

0

I am trying to write a customer validation with the goal of passing two values from the Event class to perform some validation logic and then add the error to the PURLValue property if it fails. I can't get it to work, am I using the the right approach, below, or is there a way to perform this at the class level and append the error to a specific property.

I am trying to add a custom model validation at the property level but need to pass in two values. Below is my class definition and validation implementation. When it runs, the "value" only picks up the PURLValue, the EventId is not even getting passed in. I can get this working at the class level but the property level is causing me issues.

Event Class:

 public class Event
    {
        public int? EventID {get;set;}
        [ValidPURL("EventID", "PURLValue")]
        public string PURLValue { get; set; }
...

    }

Validation Class

 [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    public sealed class ValidPURL : ValidationAttribute
    {
        private const string _defaultErrorMessage = "Web address already exist.";

        private readonly object _typeId = new object();

        public ValidPURL(int eventID, string purlValue)
            : base(_defaultErrorMessage)
        {
            EventID = eventID;
            PURLValue = purlValue;
        }

    public int EventID
    {
        get;
        private set;
    }

    public string PURLValue
    {
        get;
        private set;
    }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            EventID, PURLValue);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object eventIDValue = properties.Find(EventID, true /* ignoreCase */).GetValue(value);
        object purlValue = properties.Find(PURLValue, true /* ignoreCase 

*/).GetValue(value);

[Some Validation Logic against the database]


return [a boolean value based on validation logic]
             }
        }

Thanks for the help!