public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var patientProvince = new Province();

            if (FirstName == null || FirstName.Trim() == "")
            {
                yield return(new ValidationResult("First Name cannot be empty or just blanks", new[] { nameof(FirstName) }));
            }
            else
            {
                FirstName = SBValidations.SBCapitaize(FirstName);
            }
            if (LastName == null || LastName.Trim() == "")
            {
                yield return(new ValidationResult("Last Name cannot be empty or just blanks", new[] { nameof(LastName) }));
            }
            else
            {
                LastName = SBValidations.SBCapitaize(LastName);
            }
            if (ProvinceCode != null)
            {
                if (ProvinceCode.Trim() != "")
                {
                    ProvinceCode = ProvinceCode.Trim().ToUpper();
                    _context     = (PatientsContext)validationContext.GetService(typeof(PatientsContext));
                    if (!_context.Province.Any(a => a.ProvinceCode == ProvinceCode))
                    {
                        yield return(new ValidationResult("Please enter valid Province Code", new[] { nameof(ProvinceCode) }));
                    }
                    else
                    {
                        patientProvince = _context.Province.Where(a => a.ProvinceCode == ProvinceCode).FirstOrDefault();
                    }
                }
            }
            if (PostalCode != null)
            {
                if (patientProvince.CountryCode == "CA")
                {
                    string firstCharInPostalCode = PostalCode.Substring(0, 1).ToUpper();
                    if (!patientProvince.FirstPostalLetter.Contains(firstCharInPostalCode))
                    {
                        yield return(new ValidationResult("Please enter suitable Canadian Postal Code for your Province Code", new[] { nameof(PostalCode) }));

                        yield return(new ValidationResult("Please enter suitable Province Code for your Canadian Postal Code", new[] { nameof(ProvinceCode) }));
                    }
                    else if (SBValidations.SBPostalCodeValidation(PostalCode))
                    {
                        PostalCode = SBValidations.SBPostalCodeFormat(PostalCode);
                    }
                    else
                    {
                        yield return(new ValidationResult("Please enter valid Canadian Postal Code", new[] { nameof(PostalCode) }));
                    }
                }
                else
                {
                    string postalCode = PostalCode;
                    if (SBValidations.SBZipCodeValidation(ref postalCode))
                    {
                        PostalCode = postalCode;
                    }
                    else
                    {
                        yield return(new ValidationResult("Please enter valid US Postal Code", new[] { nameof(PostalCode) }));
                    }
                }
            }
            if (Ohip != null)
            {
                Ohip = Ohip.ToUpper();
                var _ohipRegEx = @"^\d{4}-\d{3}-\d{3}-[A-Z]{2}$";
                if (!Regex.Match(Ohip, _ohipRegEx).Success)
                {
                    yield return(new ValidationResult("OHIP, if provided must match pattern; 1234-123-123-XX", new[] { nameof(Ohip) }));
                }
            }
            if (HomePhone != null)
            {
                HomePhone = SBValidations.SBExtractDigits(HomePhone);
                if (HomePhone.Length != 10)
                {
                    yield return(new ValidationResult("Enter valid phone number", new[] { nameof(HomePhone) }));
                }
                HomePhone = string.Format("{0:###-###-####}", long.Parse(HomePhone));
            }
            if (DateOfBirth != null || DateOfBirth.ToString().Trim() != "")
            {
                if (DateOfBirth > DateTime.Now)
                {
                    yield return(new ValidationResult("Date of birth can't be in the future", new[] { nameof(DateOfBirth) }));
                }
            }
            if (Deceased)
            {
                if (DateOfDeath == null || DateOfDeath.ToString().Trim() == "")
                {
                    yield return(new ValidationResult("Date of death is required if the person is deceased", new[] { nameof(DateOfDeath) }));
                }
                else if (DateOfDeath.Value > DateTime.Now || DateOfDeath.Value < DateOfBirth)
                {
                    yield return(new ValidationResult("Date of death can't be before date of birth or in the future", new[] { nameof(DateOfDeath) }));
                }
            }
            else
            {
                if (!(DateOfDeath == null) || !(DateOfDeath.ToString().Trim() == ""))
                {
                    yield return(new ValidationResult("Date of death is not required", new[] { nameof(DateOfDeath) }));
                }
            }
            if (Gender == null || Gender.Trim() == "")
            {
                yield return(new ValidationResult("Gender cannot be empty or just blanks", new[] { nameof(Gender) }));
            }
            else
            {
                Gender = SBValidations.SBCapitaize(Gender).Substring(0, 1);
                if (!Gender.Equals("M") && !Gender.Equals("F") && !Gender.Equals("X"))
                {
                    yield return(new ValidationResult("Please enter valid gender among (M, F, X)", new[] { nameof(Gender) }));
                }
            }
            if (Address != null)
            {
                if (Address.Trim() != "")
                {
                    Address = SBValidations.SBCapitaize(Address);
                }
            }
            if (City != null)
            {
                if (City.Trim() != "")
                {
                    City = SBValidations.SBCapitaize(City);
                }
            }

            yield return(ValidationResult.Success);
        }
Beispiel #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            //throw new NotImplementedException();

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

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

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

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


            FirstName = KAValidations.KACapitalize(FirstName);
            LastName  = KAValidations.KACapitalize(LastName);
            Address   = KAValidations.KACapitalize(Address);
            City      = KAValidations.KACapitalize(City);
            Gender    = KAValidations.KACapitalize(Gender);


            if (!String.IsNullOrEmpty(ProvinceCode))
            {
                ProvinceCode = ProvinceCode.ToUpper();
            }


            if (!String.IsNullOrEmpty(Ohip))
            {
                Ohip = KAValidations.KACapitalize(Ohip);

                if (!KAValidations.KAOhipValidation(Ohip))
                {
                    yield return(new ValidationResult("Ohip format not accepted. Accepted format is 1234-123-123-XX",
                                                      new[] { nameof(Ohip) }));
                }
            }

            if (!String.IsNullOrEmpty(HomePhone))
            {
                string phoneDigits = KAValidations.KAExtractDigits(HomePhone);

                if (Regex.IsMatch(phoneDigits, @"^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"))
                {
                    phoneDigits = phoneDigits.Insert(3, "-");
                    phoneDigits = phoneDigits.Insert(7, "-");



                    HomePhone = phoneDigits;
                }

                else
                {
                    yield return(new ValidationResult("Home Phone number format is wrong. Home phone number must contain exactly 10 digits",
                                                      new[] { nameof(HomePhone) }));
                }
            }


            if (!String.IsNullOrEmpty(DateOfBirth.ToString()))
            {
                if (DateOfBirth > DateTime.Now)
                {
                    yield return(new ValidationResult("Date of birth cannot be in future",
                                                      new[] { nameof(DateOfBirth) }));
                }
            }


            if (Deceased)
            {
                if (String.IsNullOrWhiteSpace(DateOfDeath.ToString()))
                {
                    yield return(new ValidationResult("Date of Death is compulsary for deceased patients",
                                                      new[] { nameof(DateOfDeath) }));
                }
            }

            if (!Deceased)
            {
                if (!String.IsNullOrWhiteSpace(DateOfDeath.ToString()))
                {
                    yield return(new ValidationResult("Cannot add date of death for patient who is not dead (deceased must be checked)",
                                                      new[] { nameof(DateOfDeath) }));
                }
            }

            if (!String.IsNullOrWhiteSpace(DateOfDeath.ToString()))
            {
                if (DateOfDeath < DateOfBirth)
                {
                    yield return(new ValidationResult("Date of death cannot be before date of birth.",
                                                      new[] { nameof(DateOfDeath) }));
                }

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

            if (String.IsNullOrWhiteSpace(Gender))
            {
                yield return(new ValidationResult("Gender is required",
                                                  new[] { nameof(Gender) }));
            }

            if (!(Gender == "M" || Gender == "F" || Gender == "X"))
            {
                yield return(new ValidationResult("Gender must be M or F or X",
                                                  new[] { nameof(Gender) }));
            }



            localPatientContext = (PatientsContext)validationContext.GetService(typeof(PatientsContext));



            if (!String.IsNullOrEmpty(ProvinceCode))
            {
                ProvinceCode = KAValidations.KACapitalize(ProvinceCode);
                //try
                //{
                if (!(localPatientContext.Province.Any(x => x.ProvinceCode == ProvinceCode)))
                {
                    yield return(new ValidationResult("This province name do not exists",
                                                      new[] { nameof(ProvinceCode) }));
                }
                //}

                //catch (Exception ex) {

                //}
            }


            if (!String.IsNullOrEmpty(PostalCode))
            {
                if (String.IsNullOrEmpty(ProvinceCode))
                {
                    yield return(new ValidationResult("In order to add/edit postal code, You need to add valid Province code ",
                                                      new[] { nameof(PostalCode), nameof(ProvinceCode) }));
                }

                PostalCode            = PostalCode.ToUpper();
                firstLetterPostalCode = PostalCode[0].ToString();
            }

            if (localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode)
                .Select(x => x.CountryCode).FirstOrDefault() == "CA")
            {
                if (ProvinceCode.ToUpper() == "ON")
                {
                    if (!(firstLetterPostalCode == "K" || firstLetterPostalCode == "L" || firstLetterPostalCode == "M" || firstLetterPostalCode == "N" || firstLetterPostalCode == "P"))
                    {
                        yield return(new ValidationResult("Postal Code and Province Name are not matching ",
                                                          new[] { nameof(PostalCode), nameof(ProvinceCode) }));
                    }
                }

                else if (ProvinceCode.ToUpper() == "QC")

                {
                    if (!(firstLetterPostalCode == "G" || firstLetterPostalCode == "H" || firstLetterPostalCode == "J"))
                    {
                        yield return(new ValidationResult("Postal Code and Province Name are not matching ",
                                                          new[] { nameof(PostalCode), nameof(ProvinceCode) }));
                    }
                }

                else
                {
                    if (!(localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode)
                          .Select(x => x.FirstPostalLetter).FirstOrDefault() == firstLetterPostalCode))
                    {
                        yield return(new ValidationResult("Postal Code and Province Name are not matching ",
                                                          new[] { nameof(PostalCode), nameof(ProvinceCode) }));
                    }
                }

                if (!KAValidations.KAPostalCodeValidation(PostalCode))
                {
                    yield return(new ValidationResult("The postal code is not as per the format",
                                                      new[] { nameof(PostalCode) }));
                }

                if (KAValidations.KAPostalCodeValidation(PostalCode))
                {
                    PostalCode = KAValidations.KAPostalCodeFormat(PostalCode);
                }
            }

            if (localPatientContext.Province.Where(x => x.ProvinceCode == ProvinceCode)
                .Select(x => x.CountryCode).FirstOrDefault() == "US")

            {
                if (!KAValidations.KAZipCodeValidation(PostalCode))
                {
                    yield return(new ValidationResult("The ZIP code is not as per the format",
                                                      new[] { nameof(PostalCode) }));
                }
            }


            yield return(ValidationResult.Success);
        }