Country 會(huì)影響address list 加載,當(dāng)country為空時(shí)會(huì)報(bào)錯(cuò)
app/code/xxx/Customer/Model/ResourceModel/Country.php
app/code/xxx/Customer/etc/di.xml
di.xml
<preference for="Magento\Directory\Model\ResourceModel\Country" type="xxx\Customer\Model\ResourceModel\Country" />
Country.php
/**
* Load country by ISO code
*
* @param \Magento\Directory\Model\Country $country
* @param string $code
* @return \Magento\Directory\Model\ResourceModel\Country
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadByCode(\Magento\Directory\Model\Country $country, $code)
{
switch (strlen($code)) {
case 2:
$field = 'iso2_code';
break;
case 3:
$field = 'iso3_code';
break;
default:
$field = 'iso2_code';
break;
}
return $this->load($country, $code, $field);
}
form和listting 隱藏國(guó)家city 等吆录,postcode的驗(yàn)證比較復(fù)雜鸡号,直接給了一個(gè)00000默認(rèn)值
app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_form.xml
app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_listing.xml
## 設(shè)置form 字段隱藏并設(shè)置默認(rèn)值
<field name="postcode" sortOrder="120" formElement="hidden">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="default" xsi:type="string">00000</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<visible>true</visible>
<label translate="true">Zip/Postal Code</label>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</settings>
</field>
## 設(shè)置list 字段隱藏
<column name="postcode" sortOrder="92">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="componentDisabled" xsi:type="boolean">true</item>
</item>
</argument>
<settings>
<filter>text</filter>
<label translate="true">Zip/Postal Code</label>
</settings>
</column>
前端編輯頁(yè)面定血, 直接屏蔽對(duì)應(yīng)字段赔癌,postcode也是直接給了一個(gè)00000默認(rèn)值
app/code/xxx/Customer/view/frontend/templates/address/edit.phtml
General 負(fù)責(zé)前端地址數(shù)據(jù)的驗(yàn)證,重新設(shè)定驗(yàn)證規(guī)則
app/code/xxx/Customer/Model/Address/Validator/General.php
/**
* @inheritdoc
*/
public function validate(AbstractAddress $address)
{
return $this->checkRequiredFields($address);
}
/**
* Check fields that are generally required.
*
* @param AbstractAddress $address
* @return array
* @throws \Zend_Validate_Exception
*/
private function checkRequiredFields(AbstractAddress $address)
{
$errors = [];
if (!\Zend_Validate::is($address->getFirstname(), 'NotEmpty')) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'firstname']);
}
if (!\Zend_Validate::is($address->getLastname(), 'NotEmpty')) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'lastname']);
}
if (!\Zend_Validate::is($address->getStreetLine(1), 'NotEmpty')) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
}
if (!\Zend_Validate::is($address->getCompany(), 'NotEmpty')) {
$errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
}
return $errors;
}