简体   繁体   English

令人困惑的数据注释验证

[英]Confusing data annotation validation

I had been struggling with this issue since morning. 自从早上以来,我一直在努力解决这个问题。 I have the following class in my domain service. 我的域服务中有以下课程。

 [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; }
    }
}

PROBLEM: I need to create a validation rule that woudl require MarketID and Affiliation ID only when CompanyTypeID is equivalent to 1, 3, 5, 9 and 11 is there someone out there who has an idea on how to solve this? 问题:我需要创建一个验证规则,仅当CompanyTypeID等于1、3、5、9和11时,才需要MarketID和关联ID。那里有人可以解决这个问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM