I'm customizing customer_account_create.
I need save taxvat as CPF/CNPJ (Brazilian attributes) so I need to validate it, then I created an observable in: etc/events.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_save_before"> <observer name="customer_validations" instance="jCustom\Cpfcnpj\Observer\CustomerValidations"/> </event> </config>
If I'm registering a customer and the validation check if the taxvat already exists in DB my observable throws an exception and shows an error in registration page. (working as expected)
If I'm registering a customer with taxvat that there is not already in DB, the validation check if there is in DB, return false, and finish without any error, [So I think at this moment its saved in db] but the validation is called again, and this time its saved in DB so will throw an exception and show an error in registration page, but the customer is registered!! (I can login and everything works)
I tried test before check in DB if there is a session active with customer attributes:
[This code check if the taxvat in session is equal the one I'm trying to save once I can change de taxvat in customer_edit page]
//important think starts here if ($this->_session->getCustomer()->getId() != "" && $this->_session->getCustomer()->getData("taxvat") == $cpfCnpj) { return true; } //check if already in DB //method finish
but the session is not null when the validation (Observable) is called at the second time
I'm testing in a fresh installation 2.3.4 without any 3rd party modules
How can I fix that issue?
@syssolutions So in this case you can get the value of the customer from the session and and also get its corresponding taxvat.
If you get this value then return true else false.
thanks
@Rahul Gupta wrote:@syssolutions So in this case you can get the value of the customer from the session and and also get its corresponding taxvat.
If you get this value then return true else false.
thanks
This is exactly what I'm doing here:
if ($this->_session->getCustomer()->getId() != "" &&$this->_session->getCustomer()->getData("taxvat") == $cpfCnpj) { return true; }
Just to remember: the validation is called twice in customer create page:
If the customer is in edit page $this->_session->getCustomer()->getData("taxvat") return the taxvat, but in registration it returns null in first time as well the second time CustomerValidations.php is called.
I would like to understand why CustomerValidations.php is called twice when registering a customer, but in edit page it doesn't.