Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Action.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
20  protected $_customerSession;
21 
26 
30  protected $accountManagement;
31 
39  public function __construct(
40  \Magento\Framework\App\Action\Context $context,
41  \Magento\Customer\Model\Session $customerSession,
44  ) {
45  $this->_customerSession = $customerSession;
46  $this->customerRepository = $customerRepository;
47  $this->accountManagement = $accountManagement;
48  parent::__construct($context);
49  }
50 
60  protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true)
61  {
62  try {
63  $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
64  } catch (NoSuchEntityException $e) {
65  return true;
66  }
67 
68  if (isset($customer)) {
69  $validationResult = $this->accountManagement->validate($customer);
70  if (!$validationResult->isValid()) {
71  if ($addErrors) {
72  foreach ($validationResult->getMessages() as $error) {
73  $this->messageManager->addErrorMessage($error);
74  }
75  }
76  if ($redirect) {
77  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
78  return $this->resultRedirectFactory->create()->setPath('customer/account/edit');
79  }
80  return false;
81  }
82  }
83  return true;
84  }
85 }
$customer
Definition: customers.php:11
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement)
Definition: Action.php:39
_preDispatchValidateCustomer($redirect=true, $addErrors=true)
Definition: Action.php:60