help me with asp.net mvc 2 custom validation attribute

Posted by Omu on Stack Overflow See other posts from Stack Overflow or by Omu
Published on 2010-06-11T12:09:54Z Indexed on 2010/06/11 12:12 UTC
Read the original article Hit count: 625

I'm trying to write a validation attribute that is going to check that at least one of the specified properties is true

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]  
    public sealed class AtLeastOneTrueAttribute : ValidationAttribute  
    {  
        private const string DefaultErrorMessage = "select at least one";

        public AtLeastOneTrueAttribute(params string[] props)  
            : base(DefaultErrorMessage)  
        {
            this.props = props;
        }

        private readonly string[] props;  

        public override string FormatErrorMessage(string name)  
        {
            return DefaultErrorMessage;
        }  

        public override bool IsValid(object value)  
        {  
            var properties = TypeDescriptor.GetProperties(value);  
            return props.Any(p => (bool) properties.Find(p, true).GetValue(value));
        }  
    }

now when I'm trying to use I can't really get specify the props after the fir , the intellisence shows me that I'm entering the ErrorMessage and only the first string is the params string[] props

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2