/// <summary>
        /// validation of the vm coming from a create/edit action
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        /// <remarks>
        /// It would be cleaner to do this in its own validation classes,
        /// but we need a bunch of IDependencies, so putting this code
        /// here is less of an hassle.
        /// </remarks>
        public bool Validate(AddressEditViewModel vm)
        {
            var validCountries = _addressConfigurationService
                                 .GetAllCountries(vm.AddressType);
            var countryTP = _addressConfigurationService
                            .GetCountry(vm.CountryId);

            if (!SubValidation(validCountries, countryTP))
            {
                return(false);
            }
            var validProvinces = _addressConfigurationService
                                 .GetAllProvinces(vm.AddressType, countryTP);

            if (validProvinces == null)
            {
                // error condition
                return(false);
            }
            var provinceTP = GetTerritory(vm.Province);

            if (validProvinces.Any())
            {
                // there may not be any configured province, and that is ok,
                // but if any is configured, we check that the one selected is valid
                if (!SubValidation(validProvinces, provinceTP))
                {
                    return(false);
                }
            }
            var validCities = _addressConfigurationService
                              .GetAllCities(vm.AddressType,
                                            // use province if it exists, otherwise country
                                            provinceTP == null ? countryTP : provinceTP);

            if (validCities == null)
            {
                // error condition
                return(false);
            }
            if (validCities.Any())
            {
                // there may not be any configured city, and that is ok,
                // but if any is configured, we check that the one selected is valid
                var cityTP = GetTerritory(vm.City);
                if (!SubValidation(validCities, cityTP))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// validation of the vm coming from a create/edit action
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        /// <remarks>
        /// It would be cleaner to do this in its own validation classes,
        /// but we need a bunch of IDependencies, so putting this code
        /// here is less of an hassle.
        /// </remarks>
        public bool Validate(AddressEditViewModel vm)
        {
            var validCountries = _addressConfigurationService
                                 .GetAllCountries(vm.AddressType);
            var countryTP = _addressConfigurationService
                            .GetCountry(vm.CountryId);

            if (!SubValidation(validCountries, countryTP))
            {
                return(false);
            }

            var provinceTP = GetTerritory(vm.Province)
                             ?? _addressConfigurationService.SingleTerritory(vm.ProvinceId);

            if (provinceTP == null)
            {
                // maybe we did not find a territory because it's not configured,
                // but we had a free text input for the province
                var provinceName = vm.Province.Trim();
                if (provinceName.Length < 2)
                {
                    // at least two characters
                    return(false);
                }
            }
            else
            {
                // check in the configuration parts if they are a valid province or not
                var adminTypePart = provinceTP.As <TerritoryAdministrativeTypePart>();
                if (adminTypePart != null)
                {
                    if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.Province)
                    {
                        var territoryAddressTypePart = provinceTP.As <TerritoryAddressTypePart>();
                        if (territoryAddressTypePart != null)
                        {
                            if (vm.AddressType == AddressRecordType.ShippingAddress)
                            {
                                if (!territoryAddressTypePart.Shipping)
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                if (!territoryAddressTypePart.Billing)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            var cityTP = GetTerritory(vm.City)
                         ?? _addressConfigurationService.SingleTerritory(vm.CityId);

            // check in the configuration parts if they are a valid city or not
            if (cityTP != null)
            {
                var adminTypePart = cityTP.As <TerritoryAdministrativeTypePart>();
                if (adminTypePart != null)
                {
                    if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.City)
                    {
                        var territoryAddressTypePart = cityTP.As <TerritoryAddressTypePart>();
                        if (territoryAddressTypePart != null)
                        {
                            if (vm.AddressType == AddressRecordType.ShippingAddress)
                            {
                                if (!territoryAddressTypePart.Shipping)
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                if (!territoryAddressTypePart.Billing)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            // https://en.wikipedia.org/wiki/List_of_postal_codes
            // we had to make the change because we first checked
            // it was just a number
            // during use we noticed that the Netherlands have PS in the cap
            // therefore changed the control over the character
            if (string.IsNullOrWhiteSpace(vm.PostalCode))
            {
                return(false);
            }
            else
            {
                foreach (char c in vm.PostalCode)
                {
                    if (!char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c))
                    {
                        vm.Errors.Add(T("Postal or ZIP code may contain only characters or digits.").Text);
                        return(false);
                    }
                }
            }
            return(true);
        }