Ejemplo n.º 1
0
        /// <summary>
        /// Validates address attributes
        /// </summary>
        /// <param name="attributesXml">Attributes in XML format</param>
        /// <returns>Warnings</returns>
        public virtual IList <string> GetAttributeWarnings(string attributesXml)
        {
            var warnings = new List <string>();

            //ensure it's our attributes
            var attributes1 = ParseAddressAttributes(attributesXml);

            //validate required address attributes (whether they're chosen/selected/entered)
            var attributes2 = _addressAttributeService.GetAllAddressAttributes();

            foreach (var a2 in attributes2)
            {
                if (a2.IsRequired)
                {
                    bool found = false;
                    //selected address attributes
                    foreach (var a1 in attributes1)
                    {
                        if (a1.Id == a2.Id)
                        {
                            var valuesStr = ParseValues(attributesXml, a1.Id);
                            foreach (string str1 in valuesStr)
                            {
                                if (!String.IsNullOrEmpty(str1.Trim()))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    //if not found
                    if (!found)
                    {
                        var notFoundWarning = string.Format(InfoMsg.ShoppingCart_SelectAttribute, a2.Name);

                        warnings.Add(notFoundWarning);
                    }
                }
            }

            return(warnings);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a value indicating whether address is valid (can be saved)
        /// </summary>
        /// <param name="address">Address to validate</param>
        /// <returns>Result</returns>
        public virtual bool IsAddressValid(Address address)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            if (String.IsNullOrWhiteSpace(address.FirstName))
            {
                return(false);
            }

            if (String.IsNullOrWhiteSpace(address.LastName))
            {
                return(false);
            }

            if (String.IsNullOrWhiteSpace(address.Email))
            {
                return(false);
            }

            if (AddressSettings.CompanyEnabled &&
                AddressSettings.CompanyRequired &&
                String.IsNullOrWhiteSpace(address.Company))
            {
                return(false);
            }

            if (AddressSettings.StreetAddressEnabled &&
                AddressSettings.StreetAddressRequired &&
                String.IsNullOrWhiteSpace(address.Address1))
            {
                return(false);
            }

            if (AddressSettings.StreetAddress2Enabled &&
                AddressSettings.StreetAddress2Required &&
                String.IsNullOrWhiteSpace(address.Address2))
            {
                return(false);
            }

            if (AddressSettings.ZipPostalCodeEnabled &&
                AddressSettings.ZipPostalCodeRequired &&
                String.IsNullOrWhiteSpace(address.ZipPostalCode))
            {
                return(false);
            }


            if (AddressSettings.CountryEnabled)
            {
                if (address.CountryId == null || address.CountryId.Value == 0)
                {
                    return(false);
                }

                var country = _countryService.GetCountryById(address.CountryId.Value);
                if (country == null)
                {
                    return(false);
                }

                if (AddressSettings.StateProvinceEnabled)
                {
                    var states = _stateProvinceService.GetStateProvincesByCountryId(country.Id);
                    if (states.Count > 0)
                    {
                        if (address.StateProvinceId == null || address.StateProvinceId.Value == 0)
                        {
                            return(false);
                        }

                        var state = states.FirstOrDefault(x => x.Id == address.StateProvinceId.Value);
                        if (state == null)
                        {
                            return(false);
                        }
                    }
                }
            }

            if (AddressSettings.CityEnabled &&
                AddressSettings.CityRequired &&
                String.IsNullOrWhiteSpace(address.City))
            {
                return(false);
            }

            if (AddressSettings.PhoneEnabled &&
                AddressSettings.PhoneRequired &&
                String.IsNullOrWhiteSpace(address.PhoneNumber))
            {
                return(false);
            }

            if (AddressSettings.FaxEnabled &&
                AddressSettings.FaxRequired &&
                String.IsNullOrWhiteSpace(address.FaxNumber))
            {
                return(false);
            }

            var attributes = _addressAttributeService.GetAllAddressAttributes();

            if (attributes.Any(x => x.IsRequired))
            {
                return(false);
            }

            return(true);
        }