Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validate.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13 
15 {
22  protected function _validateCustomer($response)
23  {
24  $customer = null;
25  $errors = [];
26 
27  try {
29  $customer = $this->customerDataFactory->create();
30 
31  $customerForm = $this->_formFactory->create(
32  'customer',
33  'adminhtml_customer',
34  [],
35  true
36  );
37  $customerForm->setInvisibleIgnored(true);
38 
39  $data = $customerForm->extractData($this->getRequest(), 'customer');
40 
41  if ($customer->getWebsiteId()) {
42  unset($data['website_id']);
43  }
44 
45  $this->dataObjectHelper->populateWithArray(
46  $customer,
47  $data,
48  \Magento\Customer\Api\Data\CustomerInterface::class
49  );
50  $submittedData = $this->getRequest()->getParam('customer');
51  if (isset($submittedData['entity_id'])) {
52  $entity_id = $submittedData['entity_id'];
53  $customer->setId($entity_id);
54  }
55  $errors = $this->customerAccountManagement->validate($customer)->getMessages();
56  } catch (\Magento\Framework\Validator\Exception $exception) {
57  /* @var $error Error */
58  foreach ($exception->getMessages(\Magento\Framework\Message\MessageInterface::TYPE_ERROR) as $error) {
59  $errors[] = $error->getText();
60  }
61  }
62 
63  if ($errors) {
64  $messages = $response->hasMessages() ? $response->getMessages() : [];
65  foreach ($errors as $error) {
66  $messages[] = $error;
67  }
68  $response->setMessages($messages);
69  $response->setError(1);
70  }
71 
72  return $customer;
73  }
74 
81  protected function _validateCustomerAddress($response)
82  {
83  $addresses = $this->getRequest()->getPost('address');
84  if (!is_array($addresses)) {
85  return;
86  }
87  foreach (array_keys($addresses) as $index) {
88  if ($index == '_template_') {
89  continue;
90  }
91 
92  $addressForm = $this->_formFactory->create('customer_address', 'adminhtml_customer_address');
93 
94  $requestScope = sprintf('address/%s', $index);
95  $formData = $addressForm->extractData($this->getRequest(), $requestScope);
96 
97  $errors = $addressForm->validateData($formData);
98  if ($errors !== true) {
99  $messages = $response->hasMessages() ? $response->getMessages() : [];
100  foreach ($errors as $error) {
101  $messages[] = $error;
102  }
103  $response->setMessages($messages);
104  $response->setError(1);
105  }
106  }
107  }
108 
114  public function execute()
115  {
116  $response = new \Magento\Framework\DataObject();
117  $response->setError(0);
118 
119  $customer = $this->_validateCustomer($response);
120  if ($customer) {
122  }
123  $resultJson = $this->resultJsonFactory->create();
124  if ($response->getError()) {
125  $response->setError(true);
126  $response->setMessages($response->getMessages());
127  }
128 
129  $resultJson->setData($response);
130  return $resultJson;
131  }
132 }
$response
Definition: 404.php:11
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
$index
Definition: list.phtml:44
$errors
Definition: overview.phtml:9