RequiredIf - use multiple for single property

Dec 18, 2011 at 7:36 AM

Is it possible to use multiple instances of the RequiredIf attribute for a property?

I am looking to do something like this:

        [RequiredIf("ScheduleType", (int)Constants.PaymentSchedule.Recurring, ErrorMessage = "The {0} is required for a recurring payment.")]
        [RequiredIf("ScheduleType", (int)Constants.PaymentSchedule.OnceOff, ErrorMessage = "The {0} is required for a once off payment.")]
        public decimal Amount { get; set; }

I currently get this build error: "Duplicate 'RequiredIf' attribute"

Is this a .NET limitation? Are there any workarounds?

Coordinator
Dec 19, 2011 at 12:58 PM

Yes, this is a .NET limitation.

The only work around is to make a new validator class and inherit from the validator that you want to duplicate. 

Remember to register your the new validator. Like in the "Complex Custom Validation" section here: http://foolproof.codeplex.com/

Apr 18, 2012 at 8:36 AM
Edited Apr 18, 2012 at 8:36 AM

This is not a .NET limitation - it depends on how you define the RequiredIfAttribute class. If you do

[AttributeUsage(AllowMultiple=true)]
public class RequiredIfAttribute : Attribute
{
   // etc
}

then it will allow multiple instances of the attribute to be used - whether the library can handle there being more than one at the moment is another matter