Validate.php 3.0 KB

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