//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 override bool Equals(object obj) { if (obj is OrganisationAddress) { obj = Create((OrganisationAddress)obj); } var address = obj as AddressModel; if (address == null) { return(false); } if (obj is OrganisationAddress) { } if ((!string.IsNullOrWhiteSpace(Address1) || !string.IsNullOrWhiteSpace(address.Address1)) && Address1?.Trim() != address.Address1?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(Address2) || !string.IsNullOrWhiteSpace(address.Address2)) && Address2?.Trim() != address.Address2?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(Address3) || !string.IsNullOrWhiteSpace(address.Address3)) && Address3?.Trim() != address.Address3?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(City) || !string.IsNullOrWhiteSpace(address.City)) && City?.Trim() != address.City?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(County) || !string.IsNullOrWhiteSpace(address.County)) && County?.Trim() != address.County?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(Country) || !string.IsNullOrWhiteSpace(address.Country)) && Country?.Trim() != address.Country?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(PostCode) || !string.IsNullOrWhiteSpace(address.PostCode)) && PostCode?.Trim() != address.PostCode?.Trim()) { return(false); } if ((!string.IsNullOrWhiteSpace(PoBox) || !string.IsNullOrWhiteSpace(address.PoBox)) && PoBox?.Trim() != address.PoBox?.Trim()) { return(false); } return(true); }
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); }
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); }