令人困惑的数据注释验证

时间:2012-05-09 07:28:56

标签: c# silverlight validation class data-annotations

从早上起,我一直在努力解决这个问题。我的域名服务中有以下课程。

 [MetadataTypeAttribute(typeof(CompanyRecord.CompanyRecordMetadata))]
 public partial class CompanyRecord
 {

     // This class allows you to attach custom attributes to properties
     // of the CompanyRecord class.
     //
     // For example, the following marks the Xyz property as a
     // required property and specifies the format for valid values:
     //    [Required]
     //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
     //    [StringLength(32)]
     //    public string Xyz { get; set; }
    internal sealed class CompanyRecordMetadata
    {
        // Metadata classes are not meant to be instantiated.
        private CompanyRecordMetadata()
        {
        }

        public Nullable<char> AccessToClearance { get; set; }

        public Nullable<char> AccessToMarketplace { get; set; }

        public Nullable<bool> Active { get; set; }

        public string AddressLine1 { get; set; }

        public string AddressLine2 { get; set; }

        public Nullable<int> AllotedHDSpace { get; set; }

        public string City { get; set; }

        public int CompanyID { get; set; }

        public Binary CompanyLogo { get; set; }

        [Required(ErrorMessage = "Company Name is required and must be unique.")]
        public string CompanyName { get; set; }

        [Range(1, Int32.MaxValue, ErrorMessage = "Company Type is required.")]
        public int CompanyTypeID { get; set; }

        [Range(1, Int32.MaxValue, ErrorMessage = "Country is required.")]
        public Nullable<int> CountryID { get; set; }

        public string Description { get; set; }

        //[RegularExpression(pattern: @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|tv|COM|ORG|NET|GOV|MIL|BIZ|INFO|MOBI|NAME|AERO|JOBS|MUSEUM|TV|Com|Org|Net|Gov|Mil|Biz|Info|Mobi|Name|Aero|Jobs|Museum|Tv)\b", ErrorMessage = "Please provide a valid email address")]
        [RegularExpression(pattern: @"(\w[-._\w]*\w@\w[-._\w]*\w\.[a-zA-z]{2,6})", ErrorMessage = "Please provide a valid email address")]
        public string EmployeeEmail { get; set; }

        //[RegularExpression(pattern: @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|tv|COM|ORG|NET|GOV|MIL|BIZ|INFO|MOBI|NAME|AERO|JOBS|MUSEUM|TV|Com|Org|Net|Gov|Mil|Biz|Info|Mobi|Name|Aero|Jobs|Museum|Tv)\b", ErrorMessage = "Please provide a valid email address")]
        //(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})
        [RegularExpression(pattern: @"(\w[-._\w]*\w@\w[-._\w]*\w\.[a-zA-z]{2,6})", ErrorMessage = "Please provide a valid email address")]
        [Required(ErrorMessage = "Email is required.")]
        public string Email { get; set; }

        [StringLength(10, ErrorMessage = "Phone Ext. should not be more than 10 chars.")]
        public string EmployeePhoneExt { get; set; }

        [StringLength(20, ErrorMessage = "Phone No. should not be more than 20 chars.")]
        [RegularExpression(pattern: @"^[0-9\-\(\) ]*[0-9\-\(\)]*$", ErrorMessage = "Please provide a valid Phone No.")]
        public string EmployeePhoneNo { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        [Range(0, int.MaxValue, ErrorMessage = "Number of projects should be lower than 2,147,483,647.")]
        public Nullable<int> NoOfProjects { get; set; }

        public Nullable<int> NoOfUsers { get; set; }

        [StringLength(10, ErrorMessage = "Phone Ext. should not be more than 10 chars.")]
        public string PhoneExt { get; set; }

        [StringLength(20, ErrorMessage = "Phone No. should not be more than 20 chars.")]
        [Required(ErrorMessage = "Phone No. is required.")]
        [RegularExpression(pattern: @"^[0-9\-\(\) ]*[0-9\-\(\)]*$", ErrorMessage = "Please provide a valid Phone No.")]
        public string PhoneNo { get; set; }

        [RegularExpression(pattern: @"^[a-zA-Z0-9\-\(\)\* ]*[a-zA-Z0-9\-\(\)\*]*$", ErrorMessage = "Postal codes cant' contain special characters except for -, (, ), * and spaces.")]            
        public string PostalCode { get; set; }

        public string Province { get; set; }

        public string Website { get; set; }

        public int MarketID { get; set; }

        public int AffiliationID { get; set; }

        public string CallLetter { get; set; }
    }
}

问题:我需要创建一个验证规则,只有当CompanyTypeID相当于1,3,5,9和11时才需要MarketID和Affiliation ID才有人知道如何解决这个问题?< / p>