/** * Determine whether zip-code is required for the country of destination * * @param string|null $countryId * @return bool */ public function isZipCodeRequired($countryId = null) { if ($countryId != null) { return !$this->_directoryData->isZipCodeOptional($countryId); } return true; }
/** * Convert collection items to select options array * * @param string|boolean $emptyLabel * @return array */ public function toOptionArray($emptyLabel = ' ') { $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']); $sort = []; foreach ($options as $data) { $name = (string) $this->_localeLists->getCountryTranslation($data['value']); if (!empty($name)) { $sort[$name] = $data['value']; } } $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale()); foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) { $name = array_search($foregroundCountry, $sort); unset($sort[$name]); $sort = [$name => $foregroundCountry] + $sort; } $options = []; foreach ($sort as $label => $value) { $option = ['value' => $value, 'label' => $label]; if ($this->helperData->isRegionRequired($value)) { $option['is_region_required'] = true; } if ($this->helperData->isZipCodeOptional($value)) { $option['is_zipcode_optional'] = true; } $options[] = $option; } if (count($options) > 0 && $emptyLabel !== false) { array_unshift($options, ['value' => '', 'label' => $emptyLabel]); } return $options; }
/** * Validate postal/zip code * Return true and skip validation if country zip code is optional * * @param array|null|string $value * @return array|bool */ public function validateValue($value) { $attribute = $this->getAttribute(); $label = __($attribute->getStoreLabel()); $countryId = $this->getExtractedData('country_id'); if ($this->directoryHelper->isZipCodeOptional($countryId)) { return true; } $errors = []; if (empty($value) && $value !== '0') { $errors[] = __('"%1" is a required value.', $label); } if (count($errors) == 0) { return true; } return $errors; }
/** * @param AddressInterface $address * @return $this */ public function initAddressForm(AddressInterface $address) { $form = $this->initForm()->getForm(); $postcode = $form->getElement('postcode'); if ($postcode) { $postcode->removeClass('required-entry')->setRequired(!$this->_directoryHelper->isZipCodeOptional($address->getCountryId())); } $form->addValues($this->addressMapper->toFlatArray($address))->setHtmlIdPrefix("_item{$address->getId()}")->setFieldNameSuffix('address[' . $address->getId() . ']'); $this->addValuesToNamePrefixElement($address->getPrefix())->addValuesToNameSuffixElement($address->getSuffix()); return $this; }
/** * Check if postcode is valid * * @param string $countryId * @param string $postcode * @return bool */ public function isValid($countryId, $postcode) { return $this->directoryHelper->isZipCodeOptional($countryId) || !empty($postcode); }