/** * Make sure customer is valid, if logged in * * By default will add error messages and redirect to customer edit form * * @param bool $redirect - stop dispatch and redirect? * @param bool $addErrors - add error messages? * @return bool|\Magento\Framework\Controller\Result\Redirect */ protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true) { try { $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId()); } catch (NoSuchEntityException $e) { return true; } if (isset($customer)) { $validationResult = $this->accountManagement->validate($customer); if (!$validationResult->isValid()) { if ($addErrors) { foreach ($validationResult->getMessages() as $error) { $this->messageManager->addError($error); } } if ($redirect) { $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true); return $this->resultRedirectFactory->create()->setPath('customer/account/edit'); } return false; } } return true; }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomer(); $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, []); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return ['error' => -1, 'message' => implode(', ', $customerErrors)]; } if (!$isCustomerNew) { return true; } $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->accountManagement->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $customer->setGroupId(GroupInterface::NOT_LOGGED_IN_ID); } //validate customer $result = $this->accountManagement->validate($customer); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'), $quote); return true; }
/** * Set and validate Customer data. Return the updated Data Object merged with the account data * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function _validateCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer) { $form = $this->_createCustomerForm($customer); // emulate request $request = $form->prepareRequest(['order' => $this->getData()]); $data = $form->extractData($request, 'order/account'); $validationResults = $this->accountManagement->validate($customer); if (!$validationResults->isValid()) { $errors = $validationResults->getMessages(); if (is_array($errors)) { foreach ($errors as $error) { $this->_errors[] = $error; } } } $data = $form->restoreData($data); foreach ($data as $key => $value) { if (!is_null($value)) { unset($data[$key]); } } $this->dataObjectHelper->populateWithArray($customer, $data, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); return $customer; }