Ejemplo n.º 1
0
        //Validate Method
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            OECContext _context = OECContext_Singleton.Context();


            //Trim all strings of leading and trailing spaces
            if (string.IsNullOrEmpty(Name) == false || string.IsNullOrWhiteSpace(Name))
            {
                Name = Name.Trim();
                //Use SKValidations.SKCapitalize to capitalize Name
                Name = SKValidations.SKCapitalize(Name);
            }
            if (Name == "")
            {
                yield return(new ValidationResult(
                                 "Name cannot be an empty string", new string[] { nameof(Name) }));
            }


            if (string.IsNullOrEmpty(Address) == false)
            {
                Address = Address.Trim();
                //Use SKValidations.SKCapitalize to capitalize Address
                Address = SKValidations.SKCapitalize(Address);
            }
            if (string.IsNullOrEmpty(Town) == false)
            {
                Town = Town.Trim();
                //Use SKValidations.SKCapitalize to capitalize Town
                Town = SKValidations.SKCapitalize(Town);
            }
            if (string.IsNullOrEmpty(County) == false)
            {
                County = County.Trim();
                //Use SKValidations.SKCapitalize to capitalize County
                County = SKValidations.SKCapitalize(County);
            }
            if (string.IsNullOrEmpty(ProvinceCode) == false)
            {
                ProvinceCode = ProvinceCode.Trim();

                Regex pattern = new Regex(@"^[a-zA-Z]{2}$");

                //Force ProvinceCode to upper before writing to database
                ProvinceCode = ProvinceCode.ToUpper();
            }

            if (string.IsNullOrEmpty(PostalCode) == false)
            {
                PostalCode = PostalCode.Trim();
            }
            if (string.IsNullOrEmpty(HomePhone) == false)
            {
                HomePhone = HomePhone.Trim();
            }
            if (string.IsNullOrEmpty(CellPhone) == false)
            {
                CellPhone = CellPhone.Trim();
            }
            if (string.IsNullOrEmpty(Email) == false)
            {
                Email = Email.Trim();
            }
            if (string.IsNullOrEmpty(Directions) == false)
            {
                Directions = Directions.Trim();
            }

            //Either town or county must be provided, both are ok, but not necessary
            if (string.IsNullOrEmpty(Town) == true && string.IsNullOrEmpty(County) == true)
            {
                yield return(new ValidationResult(
                                 "At least one of Town or County must be provided.", new string[] { nameof(Town), nameof(County) }));
            }

            //If email is not provided, address and postal code must be provided
            if (string.IsNullOrEmpty(Email) == true &&
                (string.IsNullOrEmpty(Address) == true || string.IsNullOrEmpty(PostalCode) == true))
            {
                yield return(new ValidationResult(
                                 "Either email, or both address and postal code must be provided.", new string[] { nameof(Email), nameof(Address), nameof(PostalCode) }));
            }


            //Validate Postal Code
            var    country     = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode);
            string countryCode = country.CountryCode;
            bool   isValid     = true;
            string postalCode  = PostalCode;

            //Validate Canadian Postal Code
            if (countryCode == "CA")
            {
                postalCode = postalCode.Trim();

                isValid = SKValidations.SKPostalCodeValidation(ref postalCode);

                if (isValid == false)
                {
                    yield return(new ValidationResult(
                                     "Postal (Zip) Code is not a valid Canadian pattern: A6A 6A6 or A6A6A6", new string[] { nameof(PostalCode) }));
                }
                else
                {
                    PostalCode = postalCode;
                }
            }
            // Validate US Zip Code
            else if (countryCode == "US")
            {
                isValid = SKValidations.SKZipCodeValidation(ref postalCode);

                if (isValid == false)
                {
                    yield return(new ValidationResult(
                                     "Postal (Zip) Code is not a valid US pattern: 12345 or 12345-1234", new string[] { nameof(PostalCode) }));
                }
                else
                {
                    PostalCode = postalCode;
                }
            }


            //If both home and cell phone not provided
            if (string.IsNullOrEmpty(HomePhone) == true && string.IsNullOrEmpty(CellPhone) == true)
            {
                yield return(new ValidationResult(
                                 "Either one of Home Phone or Cell Phone is required", new string[] { nameof(HomePhone), nameof(CellPhone) }));
            }

            //If home phone provided
            if (string.IsNullOrWhiteSpace(HomePhone) == false)
            {
                //Extract phone number
                string numString = "";

                foreach (char c in HomePhone)
                {
                    if (char.IsDigit(c))
                    {
                        numString += c;
                    }
                }

                //Check if phone number is 10 digits long
                if (numString.Length != 10)
                {
                    yield return(new ValidationResult(
                                     $"Not a valid phone number, must be 10 digits.", new string[] { nameof(HomePhone) }));
                }
                //Format Cell Phone number before writing to Database
                else
                {
                    HomePhone = String.Format("{0:###-###-####}", double.Parse(numString));
                }
            }

            if (string.IsNullOrWhiteSpace(CellPhone) == false)
            {
                //Check if phone number is 10 digits long
                string numString = "";

                foreach (char c in CellPhone)
                {
                    if (char.IsDigit(c))
                    {
                        numString += c;
                    }
                }

                if (numString.Length != 10)
                {
                    yield return(new ValidationResult(
                                     $"Not a valid phone number, must be 10 digits.", new string[] { nameof(CellPhone) }));
                }
                //Format Cell Phone number before writing to Database
                else
                {
                    CellPhone = String.Format("{0:###-###-####}", double.Parse(numString));
                }
            }

            //Last Contact Date cannot be provided unless DateJoined is available (but the reverse is allowed)
            if (LastContactDate != null && DateJoined == null)
            {
                yield return(new ValidationResult(
                                 "Last Contact date cannot be provided unless Date Joined is available", new string[] { nameof(LastContactDate) }));
            }

            //A farmer cannot be contacted before they have joined the program.
            if (DateJoined != null && LastContactDate != null && (DateJoined > LastContactDate))
            {
                yield return(new ValidationResult(
                                 "A farmer cannot be contacted before they have joined the program.", new string[] { nameof(LastContactDate) }));
            }

            yield return(ValidationResult.Success);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            PatientsContext _context = new PatientsContext();

            if (string.IsNullOrEmpty(FirstName) || FirstName == " ")
            {
                yield return(new ValidationResult("First name is a required field and cannot be blank spaces", new[] { "FirstName" }));
            }
            else
            {
                FirstName = FirstName.Trim();
                FirstName = MBValidations.MBCapitalize(FirstName);
            }
            if (string.IsNullOrEmpty(LastName) || LastName == " ")
            {
                yield return(new ValidationResult("Last name is a required field and should not be blank space", new[] { "LastName" }));
            }
            else
            {
                LastName = LastName.Trim();
                LastName = MBValidations.MBCapitalize(LastName);
            }
            if (string.IsNullOrEmpty(Gender) || Gender == " ")
            {
                yield return(new ValidationResult("Gender is a required field and should not start with a blank space", new[] { "Gender" }));
            }
            else
            {
                Gender = Gender.Trim();
                Gender = MBValidations.MBCapitalize(Gender);
            }
            if (!string.IsNullOrEmpty(Address))
            {
                Address = Address.Trim();
                Address = MBValidations.MBCapitalize(Address);
            }
            if (!string.IsNullOrEmpty(City))
            {
                City = City.Trim();
                City = MBValidations.MBCapitalize(City);
            }
            if (!string.IsNullOrEmpty(ProvinceCode))
            {
                ProvinceCode = ProvinceCode.Trim();
                Province Pro   = new Province();
                string   error = "";
                ProvinceCode = ProvinceCode.ToUpper();
                try
                {
                    Pro = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).FirstOrDefault();
                    //var country = Pro.CountryCode;
                }
                catch (Exception e)
                {
                    error = e.GetBaseException().Message;
                }
                if (Pro == null)
                {
                    yield return(new ValidationResult(error, new[] { nameof(ProvinceCode) }));
                }
                else
                {
                    if (PostalCode != null)
                    {
                        PostalCode = PostalCode.Trim();
                        bool   val       = false;
                        string USZipCode = PostalCode;
                        if (Pro.CountryCode == "CA")
                        {
                            var    x       = Pro.FirstPostalLetter;
                            char[] charArr = x.ToCharArray();
                            foreach (char ch in charArr)
                            {
                                if (Convert.ToChar(PostalCode.Substring(0, 1).ToUpper()) == ch)
                                {
                                    //if(PostalCode.StartsWith(ch))
                                    val = true;
                                }
                            }
                            if (!val)
                            {
                                yield return(new ValidationResult("Postal code entered is not a valid code for the selected province", new[] { "PostalCode" }));
                            }
                            if (!MBValidations.MBPostalCodeValidation(PostalCode))
                            {
                                yield return(new ValidationResult("Postal code entered is not in valid format", new[] { "PostalCode" }));
                            }
                            else
                            {
                                PostalCode = MBValidations.MBPostalCodeFormat(PostalCode);
                            }
                        }
                        if (Pro.CountryCode == "US")
                        {
                            if (!MBValidations.MBZipCodeValidation(ref USZipCode))
                            {
                                yield return(new ValidationResult("Zip code entered is not in valid format", new[] { "PostalCode" }));
                            }
                            else
                            {
                                PostalCode = USZipCode;
                            }
                        }
                    }
                }
            }

            if (Ohip != null)
            {
                Ohip = Ohip.ToUpper();
                Regex pattern = new Regex(@"^\d\d\d\d-\d\d\d-\d\d\d-[a-z][a-z]$", RegexOptions.IgnoreCase);
                if (!pattern.IsMatch(Ohip))
                {
                    yield return(new ValidationResult("The value for OHIP entered is not in valid format", new[] { "Ohip" }));
                }
            }
            if (HomePhone != null)
            {
                HomePhone = HomePhone.Trim();
                if (HomePhone.Length != 10)
                {
                    yield return(new ValidationResult("The Home Phone Number entered should be exactly 10 digits", new[] { "HomePhone" }));
                }
                HomePhone = HomePhone.Insert(3, "-");
                HomePhone = HomePhone.Insert(7, "-");
            }
            if (DateOfBirth != null)
            {
                if (DateOfBirth > DateTime.Now)
                {
                    yield return(new ValidationResult("The date of Birth cannot be greater than current date", new[] { "DateOfBirth" }));
                }
            }
            if (Deceased)
            {
                if (DateOfDeath == null)
                {
                    yield return(new ValidationResult("The date of death is required if the deceased checkbox is chec", new[] { "DateOfDeath" }));
                }
            }
            else
            {
                DateOfDeath = null;
            }
            if (DateOfDeath != null)
            {
                if (DateOfDeath > DateTime.Now || DateOfDeath < DateOfBirth)
                {
                    yield return(new ValidationResult("The date of death cannot be greater than current date and before the Date of birth", new[] { "DateOfBirth" }));
                }
            }

            if (Gender != "M" && Gender != "F" && Gender != "X")
            {
                yield return(new ValidationResult("The value for gender entered must be M, F or X", new[] { "Gender" }));
            }

            yield return(ValidationResult.Success);
        }
Ejemplo n.º 3
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (FirstName.Trim().Length == 0)
            {
                yield return(new ValidationResult("FirstName cannot be empty or blank ", new[] { "FirstName" }));
            }
            if (LastName.Trim().Length == 0)
            {
                yield return(new ValidationResult("FirstName cannot be empty or blank ", new[] { "LastName" }));
            }

            if (UseCanadaPost == true)
            {
                if (string.IsNullOrEmpty(Street))
                {
                    yield return(new ValidationResult("Street is Required ", new[] { "Street" }));
                }
                if (string.IsNullOrEmpty(City))
                {
                    yield return(new ValidationResult("City is Required ", new[] { "City" }));
                }
                Street = Street.Trim();
                City   = City.Trim();
            }
            if (UseCanadaPost == false)
            {
                if (string.IsNullOrEmpty(Email))
                {
                    yield return(new ValidationResult("Email is Required ", new[] { "Email" }));
                }
                else
                {
                    Email = Email.Trim();
                }
            }



            // MemberId = _context.Member.Max(a => a.MemberId) + 1;
            //var memberid = _context.Member.Where(a => a.MemberId == MemberId).FirstOrDefault();
            //if (memberid != null)
            //{
            //    yield return new ValidationResult("Member is already on file", new[] { "MemberId" });
            //}
            ProvinceCode = ProvinceCode.Trim();
            ProvinceCode = ProvinceCode.ToUpper();
            if (ProvinceCode.Length != 2)
            {
                yield return(new ValidationResult("ProvinceCode length is not match ", new[] { "ProvinceCode" }));
            }
            var province = _context.Member.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault();

            if (province == null)
            {
                yield return(new ValidationResult("ProvinceCode is not match ", new[] { "ProvinceCode" }));
            }
            if (!string.IsNullOrEmpty(PostalCode))
            {
                PostalCode = PostalCode.ToUpper().Trim();
                Regex PostalValidation = new Regex(@"^(?!.*[DFIOQU])[A-VXY][0-9][A-Z][0-9][A-Z][0-9]$");
                if (!PostalValidation.IsMatch(PostalCode))
                {
                    yield return(new ValidationResult("PostalCode is not match ", new[] { "PostalCode" }));
                }
                else
                {
                    PostalCode = PostalCode.Insert(3, " ");
                }
            }
            if (!string.IsNullOrEmpty(HomePhone))
            {
                HomePhone = HomePhone.Trim();
                Regex PhoneValidation = new Regex(@"^\(?[0-9]{3}(\-|\)) ?[0-9]{3}-[0-9]{4}$");


                if (HomePhone.Length != 10 && !PhoneValidation.IsMatch(HomePhone))
                {
                    yield return(new ValidationResult("Home Phone should only be 10 digits", new[] { "HomePhone" }));
                }
                else
                {
                    HomePhone = HomePhone.Insert(3, "-");
                    HomePhone = HomePhone.Insert(7, "-");
                }
            }

            if (!string.IsNullOrEmpty(SpouseFirstName))
            {
                if (!string.IsNullOrEmpty(SpouseLastName))
                {
                    SpouseFirstName = SpouseFirstName.Trim();
                    SpouseLastName  = SpouseLastName.Trim();
                    FullName        = LastName + ", " + FirstName + "&" + SpouseLastName + ", " + SpouseFirstName;
                }
                else
                {
                    FullName = LastName + ", " + FirstName + "&" + SpouseFirstName;
                }
            }
            else
            {
                FirstName = FirstName.Trim();
                LastName  = LastName.Trim();
                FullName  = LastName + ", " + FirstName;
            }
        }
Ejemplo n.º 4
0
        public string GetAllProperties()
        {
            string fullName = (Name.Trim() == "" ? ("") : Name.Trim())
                              + (MiddleName.Trim() == "" ? ("") : " " + MiddleName.Trim())
                              + (Surname.Trim() == "" ? ("") : " " + Surname.Trim());

            string generalInfo = (fullName == "" ? ("") : fullName + "\r\n")
                                 + (Nickname == "" ? ("") : Nickname.Trim() + "\r\n")
                                 + (Title == "" ? ("") : Title.Trim() + "\r\n")
                                 + (Company == "" ? ("") : Company.Trim() + "\r\n")
                                 + (Address.Trim() == "" ? ("") : Address.Trim() + "\r\n");

            string phones = (HomePhone == "" ? ("") : "H: " + HomePhone.Trim() + "\r\n")
                            + (MobilePhone == "" ? ("") : "M: " + MobilePhone.Trim() + "\r\n")
                            + (WorkPhone == "" ? ("") : "W: " + WorkPhone.Trim() + "\r\n")
                            + (Fax == "" ? ("") : "F:" + Fax.Trim() + "\r\n");

            string emails = (Email1 == "" ? ("") : "" + Email1.Trim() + "\r\n")
                            + (Email2 == "" ? ("") : "" + Email2.Trim() + "\r\n")
                            + (Email3 == "" ? ("") : "" + Email3.Trim() + "\r\n")
                            + (Homepage == "" ? ("") : "Homepage:" + "\r\n" + Homepage.Trim() + "\r\n");

            string dateOfBirth = "";

            if (DayOfBirth != "-" && DayOfBirth != "0" || MonthOfBirth != "-" ||
                YearOfBirth != "")
            {
                dateOfBirth = "Birthday "
                              + (DayOfBirth == "-" || DayOfBirth == "0" ? ("") : DayOfBirth + ". ")
                              + (MonthOfBirth == "-" ? ("") : MonthOfBirth + " ")
                              + (YearOfBirth == "" ? ("") : YearOfBirth + " (" +
                                 (DateTime.Now.Year - Convert.ToInt32(YearOfBirth)).ToString() + ")" + "\r\n");
            }

            string dateOfAnn = "";

            if (DayOfAnn != "-" && DayOfAnn != "0" || MonthOfAnn != "-" ||
                YearOfAnn != "")
            {
                dateOfAnn = "Anniversary "
                            + (DayOfAnn == "-" ? ("") : DayOfAnn + ". ")
                            + (MonthOfAnn == "-" ? ("") : MonthOfAnn + " ")
                            + (YearOfAnn == "" ? ("") : YearOfAnn + " (" +
                               (DateTime.Now.Year - Convert.ToInt32(YearOfAnn)).ToString() + ")\r\n");
            }

            string dates = (dateOfBirth == "" ? ("") : dateOfBirth)
                           + (dateOfAnn == "" ? ("") : dateOfAnn);

            string additional = (Address2 == "" ? ("") : Address2.Trim() + "\r\n")
                                + (Phone2 == "" ? ("") : "\r\nP: " + Phone2.Trim() + "\r\n")
                                + (Notes == "" ? ("") : "\r\n" + Notes.Trim());

            string allProperties = ((generalInfo == "" ? ("") : generalInfo)
                                    + (phones == "" ? ("") : "\r\n" + phones)
                                    + (emails == "" ? ("") : "\r\n" + emails)
                                    + (dates == "" ? ("") : "\r\n" + dates)
                                    + (additional == "" ? ("") : "\r\n" + additional)).Trim();

            return(allProperties);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (FirstName == null)
            {
                FirstName = string.Empty;
            }
            else
            {
                FirstName = FirstName.Trim();
                FirstName = ASValidations.ASCapitalize(FirstName).ToString();
            }

            if (LastName == null)
            {
                LastName = string.Empty;
            }
            else
            {
                LastName = LastName.Trim();
                LastName = ASValidations.ASCapitalize(LastName).ToString();
            }

            if (Gender == null)
            {
                Gender = string.Empty;
            }
            else
            {
                Gender = Gender.Trim();
                Gender = ASValidations.ASCapitalize(Gender).ToString();
            }

            if (Address == null)
            {
                Address = string.Empty;
            }
            else
            {
                Address = Address.Trim();
                Address = ASValidations.ASCapitalize(Address).ToString();
            }

            if (City == null)
            {
                City = string.Empty;
            }
            else
            {
                City = City.Trim();
                City = ASValidations.ASCapitalize(City).ToString();
            }

            if (ProvinceCode != null)
            {
                var provSearch = _context.Province.Where(p => p.ProvinceCode == ProvinceCode).FirstOrDefault();

                if (provSearch == null)
                {
                    yield return(new ValidationResult("Province code is not on file", new[] { nameof(ProvinceCode) }));

                    yield return(new ValidationResult("Province code is required to validate Postal Code", new[] { nameof(PostalCode) }));
                }
                else
                {
                    var countrySearch = _context.Country.Where(a => a.CountryCode == provSearch.CountryCode).FirstOrDefault();

                    if (provSearch.CountryCode == "CA")
                    {
                        ProvinceCode = ProvinceCode.Trim();
                        ProvinceCode = ProvinceCode.ToUpper();

                        if (PostalCode != null || PostalCode == "")
                        {
                            PostalCode = PostalCode.Trim().ToUpper();
                            if (provSearch.FirstPostalLetter.Contains(PostalCode.Substring(0, 1)) == false)
                            {
                                yield return(new ValidationResult("First letter of postal code not valid for given province", new[] { nameof(PostalCode) }));
                            }
                            else
                            {
                                if (ASValidations.ASPostalCodeValidation(PostalCode) == false)
                                {
                                    yield return(new ValidationResult("Postal code not in cdn format: A3A 3A3", new[] { nameof(PostalCode) }));
                                }
                                else
                                {
                                    PostalCode = string.Format("{0} {1}", PostalCode.Substring(0, 3), PostalCode.Substring(3));
                                }
                            }
                        }
                    }
                    else if (provSearch.CountryCode == "US")
                    {
                        ProvinceCode = ProvinceCode.Trim();
                        if (!String.IsNullOrEmpty(PostalCode))
                        {
                            PostalCode = PostalCode.Trim();
                            if (!ASValidations.ASZipCodeValidation(PostalCode))
                            {
                                yield return(new ValidationResult("US Zip code format incorrect: 12345 / 12345-6789", new[] { nameof(PostalCode) }));
                            }
                        }
                    }
                }
            }



            if ((Deceased == true) && (DateOfDeath == null))
            {
                yield return(new ValidationResult("If deceased is true, a date of death is required", new[] { nameof(DateOfDeath) }));
            }
            else if ((Deceased == false) && (DateOfDeath != null))
            {
                yield return(new ValidationResult("Deceased must be true, if Date of Death is provided", new[] { nameof(Deceased) }));
            }

            if (Ohip != null)
            {
                Ohip = Ohip.ToUpper();
            }

            if (HomePhone != null)
            {
                HomePhone = HomePhone.Trim();
                var HomePhoneNumber = ASValidations.ASExtractDigits(HomePhone).ToString();
                if (HomePhoneNumber.Length != 10)
                {
                    yield return(new ValidationResult("Home Phone if provided, should be 10 digits: 123-123-1234", new[] { nameof(HomePhone) }));
                }
                else
                {
                    HomePhone = string.Format("{0}-{1}-{2}", HomePhoneNumber.Substring(0, 3), HomePhoneNumber.Substring(3, 3), HomePhoneNumber.Substring(6));
                }
            }



            yield return(ValidationResult.Success);
        }
Ejemplo n.º 6
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            // first name
            FirstName = FirstName.Trim();
            FirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(FirstName);

            // last name
            LastName = LastName.Trim();
            LastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(LastName);

            // spouse first name
            SpouseFirstName = SpouseFirstName.Trim();
            SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName);

            // spouse last name
            SpouseLastName = SpouseLastName.Trim();
            SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName);

            if (String.IsNullOrEmpty(SpouseFirstName))
            {
                SpouseFirstName = null;
            }
            else
            {
                SpouseFirstName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseFirstName.Trim());
            }
            if (String.IsNullOrEmpty(SpouseLastName))
            {
                SpouseLastName = null;
            }
            else
            {
                SpouseLastName = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(SpouseLastName.Trim());
            }

            // street
            Street = Street.Trim();
            Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street);

            if (String.IsNullOrEmpty(Street))
            {
                Street = null;
            }
            else
            {
                Street = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(Street.Trim());
            }

            // city
            City = City.Trim();
            City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City);

            if (String.IsNullOrEmpty(City))
            {
                City = null;
            }
            else
            {
                City = HMBPClassLibrary.HMBPValidations.HMBPCapitalize(City.Trim());
            }

            // postal code
            if (PostalCode == null || PostalCode == "")
            {
                PostalCode = "";
            }
            else
            {
                PostalCode = PostalCode.Trim();
                if (HMBPClassLibrary.HMBPValidations.HMBPPostalCodeValidation(PostalCode))
                {
                    PostalCode = HMBPClassLibrary.HMBPValidations.HMBPPostalCodeFormat(PostalCode);
                }
                else
                {
                    string _postalCode = "";
                    _postalCode = PostalCode;
                    if (HMBPClassLibrary.HMBPValidations.HMBPZipCodeValidation(ref _postalCode))
                    {
                        PostalCode = _postalCode;
                    }
                    else
                    {
                        yield return(new ValidationResult("error", new[] { "PostalCode" }));
                    }
                }
                if (ProvinceCode == null)
                {
                    yield return(new ValidationResult("error", new[] { "ProvinceCode" }));
                }
            }

            // email
            if (string.IsNullOrEmpty(Email))
            {
                Email = null;
            }
            else // trim
            {
                Email = Email.Trim();
            }

            // comment
            if (string.IsNullOrEmpty(Comment))
            {
                Comment = null;
            }
            else // trim
            {
                Comment = Comment.Trim();
            }

            //home phone reformat
            HomePhone = HMBPClassLibrary.HMBPValidations.HMBPExtractDigits(HomePhone.Trim());
            if (HomePhone.Length != 10)
            {
                yield return(new ValidationResult("The home phone can only contain 10 digits",
                                                  new[] { nameof(HomePhone) }));
            }
            else
            {
                HomePhone = HomePhone.Insert(3, "-").Insert(7, "-");
            }

            //validate Joined year
            if (YearJoined.HasValue)
            {
                if (YearJoined > DateTime.Now.Year)
                {
                    YearJoined = null;
                    yield return(new ValidationResult("The year cant be in the future", new[] { nameof(YearJoined) }));
                }
            }

            //Full name
            if (String.IsNullOrEmpty(SpouseFirstName) && String.IsNullOrEmpty(SpouseLastName))
            {
                FullName = LastName + ", " + FirstName;
            }
            else if (!String.IsNullOrEmpty(SpouseFirstName))
            {
                if (String.IsNullOrEmpty(SpouseLastName) || SpouseLastName == LastName)
                {
                    FullName = LastName + ", " + FirstName + " & " + SpouseFirstName;
                }
                else if (!String.IsNullOrEmpty(SpouseLastName))
                {
                    FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName;
                }
            }

            yield return(ValidationResult.Success);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            OECContext _context = OEC_Singleton.Context();

            FarmId = Convert.ToInt32(FarmId.ToString().Trim());
            if (Name != null)
            {
                Name = Name.Trim();
                Name = HKValidations.HKCapitalize(Name);
            }
            if (Address != null)
            {
                Address = Address.Trim();
                Address = HKValidations.HKCapitalize(Address);
            }
            if (Town != null)
            {
                Town = Town.Trim();
                Town = HKValidations.HKCapitalize(Town);
            }
            if (County != null)
            {
                County = County.Trim();
                County = HKValidations.HKCapitalize(County);
            }
            if (ProvinceCode != null)
            {
                ProvinceCode = ProvinceCode.Trim();
                ProvinceCode = ProvinceCode.ToUpper();
            }
            if (PostalCode != null)
            {
                PostalCode = PostalCode.Trim();
            }
            if (HomePhone != null)
            {
                HomePhone = HomePhone.Trim();
            }
            if (CellPhone != null)
            {
                CellPhone = CellPhone.Trim();
            }
            if (Email != null)
            {
                Email = Email.Trim();
            }
            if (Directions != null)
            {
                Directions = Directions.Trim();
            }

            if (String.IsNullOrWhiteSpace(Name) || String.IsNullOrWhiteSpace(ProvinceCode))
            {
                if (String.IsNullOrWhiteSpace(Name))
                {
                    yield return(new ValidationResult("Name is required", new[] { "Name" }));
                }
                if (String.IsNullOrWhiteSpace(ProvinceCode))
                {
                    yield return(new ValidationResult("Province Code is required", new[] { "ProvinceCode" }));
                }
            }

            if (String.IsNullOrWhiteSpace(Town) && String.IsNullOrWhiteSpace(County))
            {
                yield return(new ValidationResult("Either the town or county must be provided.", new[] { "Town", "County" }));
            }

            if (String.IsNullOrWhiteSpace(Email))
            {
                if (string.IsNullOrWhiteSpace(Address))
                {
                    yield return(new ValidationResult("Address must be provided.", new[] { "Address" }));
                }
                if (string.IsNullOrWhiteSpace(PostalCode))
                {
                    yield return(new ValidationResult("Postal Code must be provided.", new[] { "PostalCode" }));
                }
            }
            if (!String.IsNullOrEmpty(PostalCode) && !String.IsNullOrWhiteSpace(ProvinceCode))
            {
                string countryCode = "";
                if (ProvinceCode.Length == 2)
                {
                    countryCode = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode).CountryCode;
                }
                else
                {
                    countryCode = _context.Province.SingleOrDefault(p => p.Name == ProvinceCode).CountryCode;
                }

                if (countryCode == "CA")
                {
                    string pCode = PostalCode;

                    if (!HKValidations.HKPostalCodeValidation(ref pCode))
                    {
                        yield return(new ValidationResult("Please enter valid postal code.", new[] { "PostalCode" }));
                    }
                    PostalCode = pCode;
                }
                else if (countryCode == "US")
                {
                    string zCode = PostalCode;
                    if (!HKValidations.HKZipCodeValidation(ref zCode))
                    {
                        yield return(new ValidationResult("Please enter valid zip code.", new[] { "PostalCode" }));
                    }
                    PostalCode = zCode;
                }
            }

            if (String.IsNullOrWhiteSpace(HomePhone) && String.IsNullOrWhiteSpace(CellPhone))
            {
                yield return(new ValidationResult("Either the home phone number or cell phone number must be provided.", new[] { "HomePhone", "CellPhone" }));
            }
            else
            {
                if (!String.IsNullOrWhiteSpace(HomePhone))
                {
                    string hPhone = HomePhone;
                    if (!HKValidations.HKPhoneNumberValidation(ref hPhone))
                    {
                        yield return(new ValidationResult("Home phone number must be 10 digits only.", new[] { "HomePhone" }));
                    }
                    HomePhone = hPhone;
                }
                if (!String.IsNullOrWhiteSpace(CellPhone))
                {
                    string cPhone = CellPhone;
                    if (!HKValidations.HKPhoneNumberValidation(ref cPhone))
                    {
                        yield return(new ValidationResult("Cell phone number must be 10 digits only.", new[] { "CellPhone" }));
                    }
                    HomePhone = cPhone;
                }
            }

            yield return(ValidationResult.Success);
        }
        //self_validation for memebr class
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            SailContext _context = new SailContext();


            //trim all strings
            FirstName = FirstName.Trim();
            LastName  = LastName.Trim();
            if (SpouseFirstName != null)
            {
                SpouseFirstName = SpouseFirstName.Trim();
            }
            if (SpouseLastName != null)
            {
                SpouseLastName = SpouseLastName.Trim();
            }
            if (Street != null)
            {
                Street = Street.Trim();
            }
            if (City != null)
            {
                City = City.Trim();
            }
            if (ProvinceCode != null)
            {
                ProvinceCode = ProvinceCode.Trim();
            }
            if (PostalCode != null)
            {
                PostalCode = PostalCode.Trim();
            }
            HomePhone = HomePhone.Trim();
            if (Email != null)
            {
                Email = Email.Trim();
            }


            //capitalize designated fields
            FirstName       = HXClassLibrary.HXValidations.HXCapitalize(FirstName);
            LastName        = HXClassLibrary.HXValidations.HXCapitalize(LastName);
            SpouseFirstName = HXClassLibrary.HXValidations.HXCapitalize(SpouseFirstName);
            SpouseLastName  = HXClassLibrary.HXValidations.HXCapitalize(SpouseLastName);
            Street          = HXClassLibrary.HXValidations.HXCapitalize(Street);
            City            = HXClassLibrary.HXValidations.HXCapitalize(City);


            // format full name
            if (SpouseFirstName == "")
            {
                FullName = LastName + ", " + FirstName;
            }
            else if (SpouseFirstName != "" && SpouseLastName != "")
            {
                if (SpouseLastName == LastName)
                {
                    FullName = LastName + ", " + FirstName + " & " + SpouseFirstName;
                }
                else
                {
                    FullName = LastName + ", " + FirstName + " & " + SpouseLastName + ", " + SpouseFirstName;
                }
            }
            else if (SpouseFirstName != "" && SpouseLastName == "")
            {
                FullName = LastName + ", " + FirstName + " & " + SpouseFirstName;
            }


            //validate province code
            Province province     = null;
            string   errorMessage = "";

            try
            {
                ProvinceCode = (ProvinceCode + "").ToUpper();
                province     = _context.Province.Find(ProvinceCode);
                if (province == null)
                {
                    errorMessage = "the province code is not on file";
                }
            }
            catch (Exception ex)
            {
                errorMessage = $"fetching provinceCode error: {ex.GetBaseException().Message}";
            }
            if (errorMessage != "")
            {
                yield return(new ValidationResult(
                                 errorMessage,
                                 new[] { "ProvinceCode" }));
            }

            //validate postal code
            if (!string.IsNullOrEmpty(PostalCode))
            {
                if (string.IsNullOrEmpty(ProvinceCode))
                {
                    yield return(new ValidationResult(
                                     "province code is required when having the postal code",
                                     new[] { "ProvinceCode" }));
                }
                else
                {
                    if (province == null)
                    {
                        yield return(new ValidationResult(
                                         "The province code is not on file",
                                         new[] { "ProvinceCode" }));
                    }
                    else
                    {
                        if (province.CountryCode == "CA")
                        {
                            if (HXClassLibrary.HXValidations.HXPostalCodeValidation(PostalCode))
                            {
                                HXClassLibrary.HXValidations.HXPostalCodeFormat(PostalCode);
                            }
                            else
                            {
                                yield return(new ValidationResult(
                                                 "the postal code is invalid in Canada (it should follow the format: A1B 1E1)",
                                                 new[] { "PostalCode" }));
                            }
                        }
                        else if (province.CountryCode == "US")
                        {
                            string postalCode = PostalCode;
                            if (HXClassLibrary.HXValidations.HXZipCodeValidation(ref postalCode))
                            {
                                PostalCode = postalCode;
                            }
                            else
                            {
                                yield return(new ValidationResult(
                                                 "the zip code is invalid in the US (it should have 5 or 9 digits)",
                                                 new[] { "PostalCode" }));
                            }
                        }
                    }
                }
            }

            //validate home phone
            if (HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Length == 10)
            {
                HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone).Insert(3, "-").Insert(6, "-");
            }
            else
            {
                HomePhone = HXClassLibrary.HXValidations.HXExtractDigits(HomePhone);
                yield return(new ValidationResult(
                                 "a valid phone number should have 10 digits",
                                 new[] { "HomePhone" }));
            }

            //validate year joined
            if (YearJoined > Convert.ToInt32(DateTime.Now.Year))
            {
                yield return(new ValidationResult(
                                 "year joined can not be in the future",
                                 new[] { "YearJoined" }));
            }

            var memberId = _context.Member.Find(MemberId);

            if (memberId == null)
            {
                if (YearJoined == null)
                {
                    yield return(new ValidationResult(
                                     "year joined can only be null for existing records.",
                                     new[] { "YearJoined" }));
                }
            }

            //validate using canada post
            if (UseCanadaPost == false)
            {
                if (string.IsNullOrEmpty(Email))
                {
                    yield return(new ValidationResult(
                                     "Must have a valid email if not using Canada Post",
                                     new[] { "Email" }));
                }
            }
            else
            {
                if (string.IsNullOrEmpty(Street) || string.IsNullOrEmpty(City) ||
                    string.IsNullOrEmpty(ProvinceCode) || string.IsNullOrEmpty(PostalCode))
                {
                    yield return(new ValidationResult(
                                     "street, city, province code, postal code can not be null when using Canada Post.",
                                     new[] { "UseCanadaPost" }));
                }
            }


            yield return(ValidationResult.Success);
        }
Ejemplo n.º 9
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            PatientsContext _context = new PatientsContext();

            //c.Non-blank firstName, lastName and gender are required
            FirstName = YKCapitalize(FirstName);
            if (FirstName == "")
            {
                yield return(new ValidationResult("First name cannot be empty or just blanks",
                                                  new[] { nameof(FirstName) }));
            }

            LastName = YKValidations.YKCapitalize(LastName);
            if (LastName == "")
            {
                yield return(new ValidationResult("Last name cannot be empty or just blanks",
                                                  new[] { nameof(LastName) }));
            }

            //Capitalise address, city and gender
            if (Address != null)
            {
                Address = YKValidations.YKCapitalize(Address);
            }

            if (City != null)
            {
                City = YKValidations.YKCapitalize(City);
            }

            //If provinceCode is not empty or null,
            if (ProvinceCode != null)
            {
                // force it to upper case
                ProvinceCode = ProvinceCode.Trim().ToUpper();

                //  i.Validate it by fetching its record from the database … error if not found
                var provinceCode = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).Any();

                if (!provinceCode)
                {
                    yield return(new ValidationResult("Province Code is not on file",
                                                      new[] { nameof(ProvinceCode) }));
                }
                else
                {
                    if (PostalCode == null)
                    {
                        yield return(new ValidationResult("Postal Code is needed when Province Code is given",
                                                          new[] { nameof(PostalCode) }));
                    }
                }
            }
            //f.postalCode is conditionally optional but, if provided:
            if (PostalCode != null)
            {
                //i.Produce an error if provinceCode is invalid / missing … it’s required to edit a postal/ zip code
                if (ProvinceCode == null)
                {
                    yield return(new ValidationResult("Province Code is needed when Postal Code is given.",
                                                      new[] { nameof(ProvinceCode) }));
                }
                else
                {
                    var countryCode = _context.Province.Where(m => m.ProvinceCode == ProvinceCode).SingleOrDefault();
                    if (countryCode != null)
                    {
                        //ii.If provinceCode indicates the patient is in Canada,
                        //   verify that the first letter of the postalCode is correct for that province.
                        if (countryCode.CountryCode == "CA")
                        {
                            if (YKValidations.YKPostalCodeValidation(PostalCode))
                            {
                                PostalCode = YKValidations.YKPostalCodeFormat(PostalCode);
                            }
                            else
                            {
                                yield return(new ValidationResult("The format of the postalCode is not correct for that province.",
                                                                  new[] { nameof(PostalCode) }));
                            }
                        }
                        else if (countryCode.CountryCode == "US")
                        {
                            //iii.Validate and format the postal / zip code using the relevant method(s) in your XXValidations Class
                            //   (error if provinceCode is invalid / missing).
                            string postalCode = PostalCode;
                            if (YKValidations.YKZipCodeValidation(ref postalCode))
                            {
                                PostalCode = postalCode;
                            }
                            else
                            {
                                yield return(new ValidationResult("The format of the postalCode is not correct for that province.",
                                                                  new[] { nameof(PostalCode) }));
                            }
                        }
                        else
                        {
                            //If not, produce a pertinent error message for both fields.
                            yield return(new ValidationResult("The first letter of the postalCode is not correct for that province.",
                                                              new[] { nameof(PostalCode) }));
                        }
                    }
                }
            }

            //g.OHIP is optional, but if provided,
            if (Ohip != null)
            {
                if (YKValidations.YKOhipValidation(Ohip))
                {
                    //shift it to upper case and ensure it’s in this pattern: 1234 - 123 - 123 - XX.
                    Ohip = YKValidations.YKOhipFormat(Ohip);
                }
                else
                {
                    yield return(new ValidationResult("OHIP, if provided, must match pattern: 1234-123-123-XX",
                                                      new[] { nameof(Ohip) }));
                }
            }

            //h.homePhone is optional, but if provided:
            //i.It must contain exactly 10 digits(discard punctuation and text like “emergency only”).
            if (HomePhone != null)
            {
                HomePhone = YKValidations.YKExtractDigits(HomePhone.Trim());
                if (HomePhone.Length == 10)
                {
                    string homePhone = HomePhone;
                    //ii.Reformat into dash notation: 519 - 748 - 5220
                    HomePhone = YKValidations.YKHomePhoneFormat(homePhone);
                }
                else
                {
                    yield return(new ValidationResult("The phone number's length is not enough or right format",
                                                      new[] { nameof(HomePhone) }));
                }
                //iii.Sorry about historical data … we didn’t have edits.They’ll be fixed on their next visit.
            }

            //i.dateOfBirth is optional but, if provided, cannot be in the future
            if (DateOfBirth != null)
            {
                if (DateOfBirth >= DateTime.Now)
                {
                    yield return(new ValidationResult("Date of Birth cannot be in the future",
                                                      new[] { nameof(DateOfBirth) }));
                }
            }

            //i.If deceased is true, dateOfDeath is required.
            //iii.If dateOfDeath is provided, it cannot be in the future or before dateOfBirth(if it is provided)
            if (Deceased)
            {
                if (DateOfDeath == null)
                {
                    yield return(new ValidationResult("Date of Death is needed when Deceased is checked",
                                                      new[] { nameof(DateOfDeath) }));
                }
                else if (DateOfBirth == null)
                {
                    yield return(new ValidationResult("Date of Birth is needed when Deceased is checked",
                                                      new[] { nameof(DateOfBirth) }));
                }
                else if (DateOfDeath >= DateTime.Now)
                {
                    yield return(new ValidationResult("Date of Death cannot be in the future",
                                                      new[] { nameof(DateOfDeath) }));
                }
                else if (DateOfDeath < DateOfBirth)
                {
                    yield return(new ValidationResult("Date of Death cannot be before Date of Birth",
                                                      new[] { nameof(DateOfDeath) }));
                }
            }
            else
            {
                //ii.If deceased is false, dateOfDeath must be null.
                if (DateOfDeath != null)
                {
                    yield return(new ValidationResult("Date of Death is not needed when Deceased is unchecked",
                                                      new[] { nameof(DateOfDeath) }));
                }
            }

            //gender is required and must be “M”, “F” or “X”.
            //Add a field-validation <span> to display error messages on Create & Edit views.
            Gender = YKValidations.YKCapitalize(Gender);
            if (Gender == null)
            {
                yield return(new ValidationResult("Gender cannot be empty or just blanks",
                                                  new[] { nameof(Gender) }));
            }
            else
            {
                if (Gender != "M" && Gender != "F" && Gender != "X")
                {
                    yield return(new ValidationResult("Gender should be M, F or X",
                                                      new[] { nameof(Gender) }));
                }
            }

            yield return(ValidationResult.Success);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            if (FirstName == null || FirstName.Trim() == "")
            {
                yield return(new ValidationResult("First name cannot be empty or just blanks",
                                                  new[] { nameof(FirstName) }));
            }
            else
            {
                FirstName = JJValidation.JJCapitalize(FirstName.Trim());
            }

            if (LastName == null || LastName.Trim() == "")
            {
                yield return(new ValidationResult("Last name cannot be empty or just blanks",
                                                  new[] { nameof(LastName) }));
            }
            else
            {
                LastName = JJValidation.JJCapitalize(LastName.Trim());
            }

            if (Gender == null || Gender.Trim() == "")
            {
                yield return(new ValidationResult("Gender cannot be empty or just blanks", new[] { nameof(Gender) }));
            }

            else if (Gender != "M" && Gender != "F" && Gender != "X" && Gender != "m" && Gender != "f" && Gender != "x")
            {
                yield return(new ValidationResult("Gender must be either 'M', 'F' or 'X'", new[] { "Gender" }));
            }

            else
            {
                Gender = JJValidation.JJCapitalize(Gender);
            }

            Address = JJValidation.JJCapitalize(Address);
            City    = JJValidation.JJCapitalize(City);



            string countryCode     = "";
            string firstPostalCode = "";

            //var patient = _context.Patient.FirstOrDefault();
            if (ProvinceCode != null)
            {
                var province = _context.Province.Where(p => p.ProvinceCode == ProvinceCode);

                if (!province.Any())
                {
                    yield return(new ValidationResult("Province Code is not on file", new[] { "ProvinceCode" }));
                }
                else
                {
                    countryCode     = province.FirstOrDefault().CountryCode;
                    firstPostalCode = province.FirstOrDefault().FirstPostalLetter;
                }
            }

            if (PostalCode != null && (ProvinceCode == null || countryCode == ""))
            {
                yield return(new ValidationResult("Province Code, if provided, is required to validate Postal Code", new[] { "ProvinceCode" }));
            }
            else if (PostalCode != null)
            {
                if (countryCode == "CA")
                {
                    if (JJValidation.JJPostalCodeValidation(PostalCode))
                    {
                        if (firstPostalCode.Contains(PostalCode.ToUpper().Substring(0, 1)))

                        //if (JJValidation.JJPostalCodeFirstChar(patient.PostalCode, patient.ProvinceCode))
                        {
                            PostalCode = JJValidation.JJPostalCodeFormat(PostalCode);
                        }
                        else
                        {
                            yield return(new ValidationResult("First letter of Postal Code is not valid for given province", new[] { "PostalCode" }));

                            yield return(new ValidationResult("Province code is not proper to first Postal Code", new[] { "ProvinceCode" }));
                        }
                    }
                    else
                    {
                        yield return(new ValidationResult("Postal Code is not code pattern: A3A 3A3", new[] { "PostalCode" }));
                    }
                }
                else
                {
                    string postcode = PostalCode;
                    if (JJValidation.JJZipCodeValidation(ref postcode))
                    {
                        PostalCode = postcode;
                    }
                    else
                    {
                        yield return(new ValidationResult("Province Code is is not code patter: 55555 or 12345-6789", new[] { "ProvinceCode" }));
                    }
                }
            }


            if (Ohip != null && Ohip.Trim() != "")
            {
                Ohip = Ohip.ToUpper();
                if (!Regex.IsMatch(Ohip, @"^\d{4}-\d{3}-\d{3}-[a-zA-Z]{2}$"))
                {
                    yield return(new ValidationResult("OHIP, if provided, must match pattern: 1234 - 123 - 123 - XX", new[] { "Ohip" }));
                }
            }

            if (HomePhone != null && HomePhone.Trim() != "")
            {
                string digitHomePhone = "";
                digitHomePhone = JJValidation.JJExtractDigits(HomePhone);
                if (digitHomePhone.Length != 10)
                {
                    yield return(new ValidationResult("Home phone, if provided,  must be 10 digits: 123-123-1234", new[] { nameof(HomePhone) }));
                }
                else
                {
                    HomePhone = digitHomePhone.Substring(0, 3) + "-" + digitHomePhone.Substring(3, 3) + "-" + digitHomePhone.Substring(6);
                }
            }

            if (DateOfBirth != null)
            {
                if (DateOfBirth > DateTime.Now)
                {
                    yield return(new ValidationResult("Date of Birth cannot be in the future", new[] { nameof(DateOfBirth) }));
                }
            }

            if (DateOfDeath != null)
            {
                if (DateOfDeath > DateTime.Now)
                {
                    yield return(new ValidationResult("Date of Death cannot be in the future", new[] { nameof(DateOfDeath) }));
                }
            }

            if (Deceased == true && DateOfDeath == null)
            {
                yield return(new ValidationResult("If Deceased is true, a Date Of Death is required", new[] { nameof(DateOfDeath) }));
            }
            else if (Deceased == false && DateOfDeath != null)
            {
                yield return(new ValidationResult("Deceased must be true if Date Of Death is provided", new[] { nameof(Deceased) }));
            }

            yield return(ValidationResult.Success);
        }
        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);
        }
Ejemplo n.º 12
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);
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            //First Name check and Capitalize
            if (string.IsNullOrEmpty(FirstName))
            {
                yield return(new ValidationResult("Requir First Name", new[] { "FirstName" }));
            }
            else
            {
                FirstName = KYValidations.KYCapitalize(FirstName);
            }

            //Last Name Check and Capitalize
            if (string.IsNullOrEmpty(LastName))
            {
                yield return(new ValidationResult("Requir Last Name", new[] { "LastName" }));
            }
            else
            {
                LastName = KYValidations.KYCapitalize(LastName);
            }

            // Address Capitalize
            Address = KYValidations.KYCapitalize(Address);

            // City Capitalize
            City = KYValidations.KYCapitalize(City);

            //  Province Code Check
            Province findProvinceinDatadabase = null;

            if (!string.IsNullOrEmpty(ProvinceCode))
            {
                ProvinceCode = ProvinceCode.ToUpper();
                string err = string.Empty;
                try
                {
                    findProvinceinDatadabase = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault();
                }
                catch (Exception e)
                {
                    err = e.GetBaseException().Message;
                }

                if (findProvinceinDatadabase == null)
                {
                    yield return(new ValidationResult("Province Code is not on file", new[] { "ProvinceCode" }));
                }
                if (!string.IsNullOrEmpty(err))
                {
                    yield return(new ValidationResult(err, new[] { "ProvinceCode" }));
                }
            }

            //postalcode check and format
            if (!string.IsNullOrEmpty(PostalCode))
            {
                if (string.IsNullOrEmpty(ProvinceCode))
                {
                    yield return(new ValidationResult("Province Code is required to validate Postal Code", new[] { "ProvinceCode", "PostalCode" }));
                }
                if (findProvinceinDatadabase != null)
                {
                    if (findProvinceinDatadabase.CountryCode == "CA")
                    {
                        //if (!PostalCode.StartsWith(findProvinceinDatadabase.FirstPostalLetter))
                        //    yield return new ValidationResult("First letter of Postal Code not valid for given province", new[] { "ProvinceCode", "PostalCode", "" });

                        if (KYValidations.KYPostalCodeValidation(PostalCode))
                        {
                            PostalCode = KYValidations.KYPostalCodeFormat(PostalCode);
                        }
                        else
                        {
                            yield return(new ValidationResult("Postal Code not CDN pattern: A3A 3A3", new[] { "PostalCode" }));
                        }
                    }
                    else if (findProvinceinDatadabase.CountryCode == "USA")
                    {
                        string localPostalCode = PostalCode;
                        if (KYValidations.KYZipCodeValidation(ref localPostalCode))
                        {
                            PostalCode = localPostalCode;
                        }
                        else
                        {
                            yield return(new ValidationResult("Zip Code is not valid", new[] { "PostalCode" }));
                        }
                    }
                }
            }

            //OHIP check
            if (!string.IsNullOrEmpty(Ohip))
            {
                Ohip = Ohip.ToUpper().Trim();
                Regex OhipValidate = new Regex(@"^\d{4}-\d{3}-\d{3}-[A-Z][A-Z]$");
                if (!OhipValidate.IsMatch(Ohip))
                {
                    yield return(new ValidationResult("OHIP. if provided, must match pattern: 1234-123-123-XX", new[] { "Ohip" }));
                }
            }

            //Date of Birth check
            if (DateOfBirth != null)
            {
                if (DateOfBirth > DateTime.Now)
                {
                    yield return(new ValidationResult("Date of Birth cannot be in the future", new[] { "DateOfBirth" }));
                }
            }

            // Date of Death check
            string popupMessage = string.Empty;

            if (Deceased)
            {
                if (DateOfDeath == null)
                {
                    popupMessage = "Deceased must be true if Date of Death is provided";
                    yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" }));
                }
                else if (DateOfDeath > DateTime.Now)
                {
                    popupMessage = "Date of Death cannot be in the future";
                    yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" }));
                }
                else if (DateOfDeath < DateOfBirth)
                {
                    popupMessage = "Date of Death cannot be before Date of Birth";
                    yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath", "DateOfBirth" }));
                }
            }
            else
            {
                if (DateOfDeath != null)
                {
                    popupMessage = "Deceased must be true if Date of Death is provided";
                    yield return(new ValidationResult(popupMessage, new[] { "Deceased" }));
                }
                if (DateOfDeath != null && DateOfDeath > DateTime.Now)
                {
                    popupMessage = "Date of Death cannot be in the future";
                    yield return(new ValidationResult(popupMessage, new[] { "DateOfDeath" }));
                }
            }

            //Phone check
            if (!string.IsNullOrEmpty(HomePhone))
            {
                HomePhone = HomePhone.Trim();
                HomePhone = KYValidations.KYExtractDigits(HomePhone);
                if (HomePhone.Length != 10)
                {
                    yield return(new ValidationResult("Home Phone, if provided, must be 10 digits: 123-123-1234", new[] { "HomePhone" }));
                }
                else
                {
                    HomePhone = HomePhone.Insert(3, "-");
                    HomePhone = HomePhone.Insert(7, "-");
                }
            }

            //Gender Check and Capitalize
            if (string.IsNullOrEmpty(Gender))
            {
                yield return(new ValidationResult("Requir Gender", new[] { "Gender" }));
            }
            else
            {
                string lowerGender = Gender.Trim().ToLower();;
                if (lowerGender != "m" && lowerGender != "f" && lowerGender != "x")
                {
                    yield return(new ValidationResult("Gender must be either 'M', 'F' or 'X'", new[] { "Gender" }));
                }
                else
                {
                    Gender = KYValidations.KYCapitalize(Gender);
                }
            }

            yield return(ValidationResult.Success);
        }
Ejemplo n.º 14
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            OECContext _context = JHOECContext_Singleton.Context();

            //trim empty spaces for all fields

            if (!string.IsNullOrEmpty(PostalCode))
            {
                PostalCode = PostalCode.Trim();
            }
            if (!string.IsNullOrEmpty(HomePhone))
            {
                HomePhone = HomePhone.Trim();
            }
            if (!string.IsNullOrEmpty(CellPhone))
            {
                CellPhone = CellPhone.Trim();
            }
            if (!string.IsNullOrEmpty(Email))
            {
                Email = Email.Trim();
            }
            if (!string.IsNullOrEmpty(Directions))
            {
                Directions = Directions.Trim();
            }

            //capitalize name
            if (!string.IsNullOrEmpty(Name.Trim()))
            {
                Name.Trim();
                Name = JHValidations.JHCapitalize(Name);
            }
            else
            {
                yield return(new ValidationResult("Name cannot be empty strings", new string[] { nameof(Name) }));
            }

            //capitalize address
            if (!string.IsNullOrEmpty(Address))
            {
                Address = Address.Trim();
                Address = JHValidations.JHCapitalize(Address);
            }
            //else
            //{
            //    yield return new ValidationResult("Address cannot be empty strings", new string[] { nameof(Address)});
            //}
            //capitalize town
            if (!string.IsNullOrEmpty(Town))
            {
                Town = Town.Trim();
                Town = JHValidations.JHCapitalize(Town);
            }
            //else
            //{
            //    yield return new ValidationResult("Town cannot be empty strings", new string[] { nameof(Town) });
            //}

            //capitalize county
            if (!string.IsNullOrEmpty(County))
            {
                County = County.Trim();
                County = JHValidations.JHCapitalize(County);
            }
            //else
            //{
            //    yield return new ValidationResult("County cannot be empty strings", new string[] { nameof(County) });
            //}

            //either town or county must be provided
            if (string.IsNullOrEmpty(County) && string.IsNullOrEmpty(Town))
            {
                yield return(new ValidationResult("Either County or Town must be provided", new[] { nameof(Town), nameof(County) }));
            }

            //if email is not provided, address and postal must be provided
            if (string.IsNullOrEmpty(Email) && string.IsNullOrEmpty(Address) || string.IsNullOrEmpty(PostalCode))
            {
                yield return(new ValidationResult("Either Email or Address and Postal Code must be provided", new[] { nameof(Address), nameof(PostalCode), nameof(Email) }));
            }

            //force province code to upper case
            if (!string.IsNullOrEmpty(ProvinceCode))
            {
                ProvinceCode = ProvinceCode.Trim();
                ProvinceCode = ProvinceCode.ToUpper();
            }

            //validate postcode based on country
            if (_context.Province.Where(p => p.ProvinceCode == ProvinceCode).Select(p => p.CountryCode).FirstOrDefault() == "CA")
            {
                var postalCodeCheck = PostalCode;
                var validateResult  = false;
                validateResult = JHValidations.JHPostalCodeValidation(ref postalCodeCheck);
                if (validateResult == true)
                {
                    PostalCode = postalCodeCheck;
                }
                else
                {
                    yield return(new ValidationResult(" Postal Code is invalid. The format doesnt match 'A3B C4B'", new[] { nameof(PostalCode) }));
                }
            }
            else if (_context.Province.Where(p => p.ProvinceCode == ProvinceCode).Select(p => p.CountryCode).FirstOrDefault() == "US")
            {
                var postalCodeCheck = PostalCode;
                var validateResult  = false;
                validateResult = JHValidations.JHZipCodeValidation(ref postalCodeCheck);
                if (validateResult == true)
                {
                    PostalCode = postalCodeCheck;
                }
                else
                {
                    yield return(new ValidationResult(" Zip Code is invalid. The format doesnt match '12345' or '12345-6789'", new[] { nameof(PostalCode) }));
                }
            }

            //last contacted date cannot before join date, AND cannot exist if the farm hasn't joined yet
            if (DateJoined == null && LastContactDate != null)
            {
                yield return(new ValidationResult("Last contacted date doesnt exist if the farm hasn't joined yet", new[] { nameof(LastContactDate), nameof(DateJoined) }));
            }
            if (DateJoined != null && LastContactDate != null)
            {
                if (DateJoined > LastContactDate)
                {
                    yield return(new ValidationResult("Last contaced date cound not exist before the join date", new[] { nameof(LastContactDate), nameof(DateJoined) }));
                }
            }

            //validate phone number
            if (string.IsNullOrEmpty(CellPhone) && string.IsNullOrEmpty(HomePhone))
            {
                yield return(new ValidationResult("Either cell phone or home phone must be provided", new[] { nameof(CellPhone), nameof(HomePhone) }));
            }
            if (!string.IsNullOrEmpty(CellPhone))
            {
                var phoneCheck     = CellPhone;
                var validateResult = false;
                validateResult = JHValidations.JHPhoneValidate(ref phoneCheck);
                if (validateResult == true)
                {
                    CellPhone = phoneCheck;
                }
                else
                {
                    yield return(new ValidationResult(" Cell phone number is invalid. The format doesnt match '123-456-7890'", new[] { nameof(CellPhone) }));
                }
            }
            if (!string.IsNullOrEmpty(HomePhone))
            {
                var phoneCheck     = HomePhone;
                var validateResult = false;
                validateResult = JHValidations.JHPhoneValidate(ref phoneCheck);
                if (validateResult == true)
                {
                    HomePhone = phoneCheck;
                }
                else
                {
                    yield return(new ValidationResult(" Home phone number is invalid. The format doesnt match '123-456-7890'", new[] { nameof(HomePhone) }));
                }
            }
            yield return(ValidationResult.Success);
        }