magento2: when switching to required suffix and/or prefix, can't save customers any longer
when configuring magento to require either suffix or prefix it’s no longer possible to save customers which do not have the (now missing) values yet. the customer model and its related addresses are being validated in the old state when the customer is saved before the new address data is supposed to be saved, thus making it impossible to ever meet the validation requirement for the addresses.
Preconditions
- magento 2.1.2
Steps to reproduce
- set magento to not require a suffix
- in adminpanel save a customer and a customer address without suffix
- configure magento to require a suffix
- try to save the customer now providing a suffix for both the customer as well as his address
Expected result
- the customer saves and now has a suffix
- the customer saves and his address has a suffix
Actual result
- an error message appears: “suffix is a required field”
Details
Validation occurs in:
Magento\Customer\Model\ResourceModel\Address at line 89
originating from
Magento\Customer\Model\ResourceModel\Customer\Relation in method processRelation at line 49
the address object being passed to this stems from
Magento\Customer\Model\ResourceModel\CustomerRepository in method save at line 149 to 158:
[...]
$origAddresses = $customer->getAddresses();
$customer->setAddresses([]);
$customerData = $this->extensibleDataObjectConverter->toNestedArray(
$customer,
[],
'\Magento\Customer\Api\Data\CustomerInterface'
);
$customer->setAddresses($origAddresses);
$customerModel = $this->customerFactory->create(['data' => $customerData]);
[...]
as you can see the customer factory is creating the customerModel from customerData which was extracted from a customer object with no addresses, so later when that model is begin saved, its validated against its old version of addresses which do not have a suffix value yet. if the addresses are being validated in relation to the save of the customer, then the addresses should be saved first thus having a chance to be valid… right? or just don’t automatically validate them at all when the customer is being saved…
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 15 (3 by maintainers)
@chrisgan btw if you set it to optional but you define a list of values you basically have the desired behaviour, the user can not leave the field empty since its a select. only thing to consider is that a person could craft a request where the data is missing…