1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Controller\Adminhtml\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Message\Error;
use Magento\Customer\Controller\Adminhtml\Index as CustomerAction;
/**
* Class for validation of customer
*/
class Validate extends CustomerAction implements HttpPostActionInterface, HttpGetActionInterface
{
/**
* Customer validation
*
* @param \Magento\Framework\DataObject $response
* @return CustomerInterface|null
*/
protected function _validateCustomer($response)
{
$customer = null;
$errors = [];
try {
/** @var CustomerInterface $customer */
$customer = $this->customerDataFactory->create();
$customerForm = $this->_formFactory->create(
'customer',
'adminhtml_customer',
[],
true
);
$customerForm->setInvisibleIgnored(true);
$data = $customerForm->extractData($this->getRequest(), 'customer');
if ($customer->getWebsiteId()) {
unset($data['website_id']);
}
$this->dataObjectHelper->populateWithArray(
$customer,
$data,
\Magento\Customer\Api\Data\CustomerInterface::class
);
$submittedData = $this->getRequest()->getParam('customer');
if (isset($submittedData['entity_id'])) {
$entity_id = $submittedData['entity_id'];
$customer->setId($entity_id);
}
$errors = $this->customerAccountManagement->validate($customer)->getMessages();
} catch (\Magento\Framework\Validator\Exception $exception) {
/* @var $error Error */
foreach ($exception->getMessages(\Magento\Framework\Message\MessageInterface::TYPE_ERROR) as $error) {
$errors[] = $error->getText();
}
}
if ($errors) {
$messages = $response->hasMessages() ? $response->getMessages() : [];
foreach ($errors as $error) {
$messages[] = $error;
}
$response->setMessages($messages);
$response->setError(1);
}
return $customer;
}
/**
* AJAX customer validation action
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$response = new \Magento\Framework\DataObject();
$response->setError(0);
$this->_validateCustomer($response);
$resultJson = $this->resultJsonFactory->create();
if ($response->getError()) {
$response->setError(true);
$response->setMessages($response->getMessages());
}
$resultJson->setData($response);
return $resultJson;
}
}