Country 会影响address list 加载,当country为空时会报错
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 隐藏国家city 等,postcode的验证比较复杂,直接给了一个00000默认值
app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_form.xml
app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_listing.xml
## 设置form 字段隐藏并设置默认值
<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>
## 设置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>
前端编辑页面, 直接屏蔽对应字段,postcode也是直接给了一个00000默认值
app/code/xxx/Customer/view/frontend/templates/address/edit.phtml
General 负责前端地址数据的验证,重新设定验证规则
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;
}