public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!Emails.Any())
            {
                yield return(new ValidationResult("An email is required."));
            }

            if (Emails.Count() > 20)
            {
                yield return(new ValidationResult("You can only invite up to 20 users at a time."));
            }

            var attr = new EmailAddressAttribute();

            for (var i = 0; i < Emails.Count(); i++)
            {
                var email = Emails.ElementAt(i);
                if (!attr.IsValid(email) || email.Contains(" ") || email.Contains("<"))
                {
                    yield return(new ValidationResult($"Email #{i + 1} is not valid.",
                                                      new string[] { nameof(Emails) }));
                }
                else if (email.Length > 256)
                {
                    yield return(new ValidationResult($"Email #{i + 1} is longer than 256 characters.",
                                                      new string[] { nameof(Emails) }));
                }
            }
        }
Exemple #2
0
 public override string ToString()
 {
     if (string.IsNullOrEmpty(FullName))
     {
         if (Emails.Count() > 0)
         {
             return(Emails.ElementAt(0));
         }
         else
         {
             return("[Empty contact]");
         }
     }
     else
     {
         return(FullName);
     }
 }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!Emails.Any())
            {
                yield return(new ValidationResult("An email is required."));
            }

            if (Emails.Count() > 20)
            {
                yield return(new ValidationResult("You can only invite up to 20 users at a time."));
            }

            var attr = new EmailAddressAttribute();

            for (int i = 0; i < Emails.Count(); i++)
            {
                if (!attr.IsValid(Emails.ElementAt(i)))
                {
                    yield return(new ValidationResult($"Email #{i + 1} is not valid.", new string[] { nameof(Emails) }));
                }
            }
        }