public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (ProvinceCode != null)
            {
                if (ProvinceCode.Length != 2)
                {
                    yield return(new ValidationResult("Province code cannot be longer than 2 characters", new[] { "ProvinceCode" }));
                }
                else
                {
                    var provinceContext = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault();
                    if (provinceContext == null)
                    {
                        yield return(new ValidationResult("Invalid province code", new[] { "ProvinceCode" }));
                    }
                    else
                    {
                        ProvinceCode = ProvinceCode.Trim().ToUpper();
                    }
                }
            }

            Regex postalCodeRegex = new Regex((@"^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}\s{0,1}[0-9]{1}[A-Za-z]{1}[0-9]{1}"), RegexOptions.IgnoreCase);

            if (PostalCode != null)
            {
                if (postalCodeRegex.IsMatch(PostalCode.Trim()))
                {
                    if (!PostalCode.Contains(" "))
                    {
                        PostalCode = PostalCode.Insert(3, " ");
                        PostalCode = PostalCode.Trim().ToUpper();
                    }
                    else
                    {
                        PostalCode = PostalCode.Trim().ToUpper();
                    }
                }
                else
                {
                    yield return(new ValidationResult("Invalid postal code. Postal code must match Canadian postal pattern", new[] { "PostalCode" }));
                }
            }

            Regex homePhoneRegex = new Regex(@"^[0-9]{3}-{0,1}[0-9]{3}-{0,1}[0-9]{4}");

            if (HomePhone != null)
            {
                if (homePhoneRegex.IsMatch(HomePhone))
                {
                    if (!HomePhone.Contains('-'))
                    {
                        HomePhone = HomePhone.Insert(3, "-");
                        HomePhone = HomePhone.Insert(7, "-");
                        HomePhone = HomePhone.Trim();
                    }
                }
                else
                {
                    yield return(new ValidationResult("Invalid Phone Number Format. It must be in the format : 999-999-9999", new[] { "HomePhone" }));
                }
            }

            if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName))
            {
                FullName = LastName.Trim() + ", " + FirstName.Trim();
            }
            else
            {
                if (SpouseLastName == null || SpouseLastName == LastName)
                {
                    FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseFirstName.Trim();
                }
                else
                {
                    FullName = LastName.Trim() + ", " + FirstName.Trim() + " & " + SpouseLastName.Trim() + ", " + SpouseFirstName.Trim();
                }
            }

            if (UseCanadaPost)
            {
                if (string.IsNullOrEmpty(Street))
                {
                    yield return(new ValidationResult("If Canada post is checked, street name is required", new[] { "Street" }));
                }
                if (string.IsNullOrEmpty(City))
                {
                    yield return(new ValidationResult("If Canada post is checked, city name is required", new[] { "City" }));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(Email))
                {
                    yield return(new ValidationResult("If Canada post is not checked, email address is required", new[] { "Email" }));
                }
            }

            if (Street != null)
            {
                Street = Street.Trim();
            }

            if (City != null)
            {
                City = City.Trim();
            }

            if (Email != null)
            {
                Email = Email.Trim();
            }

            if (Comment != null)
            {
                Comment = Comment.Trim();
            }

            if (LastName != null)
            {
                LastName = LastName.Trim();
            }

            if (FirstName != null)
            {
                FirstName = FirstName.Trim();
            }

            if (SpouseFirstName != null)
            {
                SpouseFirstName = SpouseFirstName.Trim();
            }

            if (SpouseLastName != null)
            {
                SpouseLastName = SpouseLastName.Trim();
            }

            //determine if editing or creating new
            var memberId = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault();

            if (memberId != null)
            {
                //yield error : member id is already on file
            }
            else
            {
                //yield error: member id not on file
            }

            yield return(ValidationResult.Success);
        }
Example #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            //if (MemberId != 0)
            //{
            //    FullName = FirstName + ' ' + LastName + " & " + SpouseFirstName;
            //}
            if (ProvinceCode != null)
            {
                if (ProvinceCode.Length != 2)
                {
                    yield return(new ValidationResult("The Province Code should be exactly 2 characters long", new[] { "ProvinceCode" }));
                }
                else
                {
                    var province = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault();
                    if (province == null)
                    {
                        yield return(new ValidationResult("The Province Code is not valid", new[] { "ProvinceCode" }));
                    }
                    else
                    {
                        ProvinceCode = ProvinceCode.Trim().ToUpper();
                    }
                }
            }
            Regex PostalCodePattern = new Regex((@"^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s{0,1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}"), RegexOptions.IgnoreCase);

            if (PostalCodePattern.IsMatch(PostalCode.Trim()))
            {
                if (!PostalCode.Contains(" "))
                {
                    PostalCode = PostalCode.Insert(3, " ");
                    PostalCode = PostalCode.Trim().ToUpper();
                }
                else
                {
                    PostalCode = PostalCode.Trim().ToUpper();
                }
            }
            else
            {
                yield return(new ValidationResult("The Postal Code entered is not in valid canadian format", new[] { "PostalCode" }));
            }


            Regex HomePhonePattern = new Regex(@"^\d\d\d-{0,1}\d\d\d-{0,1}\d\d\d\d");

            if (HomePhone != null)
            {
                if (HomePhonePattern.IsMatch(HomePhone))
                {
                    if (!HomePhone.Contains('-'))
                    {
                        HomePhone = HomePhone.Insert(3, "-");
                        HomePhone = HomePhone.Insert(7, "-");
                        HomePhone = HomePhone.Trim();
                    }
                }
                else
                {
                    yield return(new ValidationResult("The home Phone number entered is not in valid format 999-999-9999", new[] { "HomePhone" }));
                }
            }

            if (string.IsNullOrEmpty(SpouseFirstName) && string.IsNullOrEmpty(SpouseLastName))
            {
                FullName = LastName.Trim() + "," + FirstName.Trim();
            }
            else
            {
                if (SpouseLastName == null || SpouseLastName == LastName)
                {
                    FullName = FirstName.Trim() + ' ' + LastName.Trim() + " & " + SpouseFirstName.Trim();
                }
                else
                {
                    FullName = LastName.Trim() + "," + FirstName.Trim() + " & " + SpouseLastName.Trim() + "," + SpouseFirstName.Trim();
                }
            }

            if (UseCanadaPost)
            {
                if (string.IsNullOrEmpty(Street) && string.IsNullOrEmpty(City))
                {
                    yield return(new ValidationResult("The Street name and City Name field are required fields if you have checked Canada Post checkbox", new[] { "Street" }));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(Email))
                {
                    yield return(new ValidationResult("The Email address field is required", new[] { "Email" }));
                }
            }
            if (MemberId == 0)
            {
                var duplicateID = _context.Member.Where(x => x.MemberId == MemberId).FirstOrDefault();
                if (duplicateID != null)
                {
                    yield return(new ValidationResult("The Member Id entered is already on file", new[] { "MemberId" }));
                }
            }
            if (Street != null)
            {
                Street = Street.Trim();
            }
            if (City != null)
            {
                City = City.Trim();
            }
            if (Email != null)
            {
                Email = Email.Trim();
            }
            if (Comment != null)
            {
                Comment = Comment.Trim();
            }
            yield return(ValidationResult.Success);
        }