Validate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Address;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\App\Action\HttpGetActionInterface;
  10. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  11. use Magento\Framework\Controller\Result\Json;
  12. use Magento\Framework\DataObject;
  13. /**
  14. * Class for validation of customer address form on admin.
  15. */
  16. class Validate extends Action implements HttpPostActionInterface, HttpGetActionInterface
  17. {
  18. /**
  19. * Authorization level of a basic admin session
  20. *
  21. * @see _isAllowed()
  22. */
  23. public const ADMIN_RESOURCE = 'Magento_Customer::manage';
  24. /**
  25. * @var \Magento\Framework\Controller\Result\JsonFactory
  26. */
  27. private $resultJsonFactory;
  28. /**
  29. * @var \Magento\Customer\Model\Metadata\FormFactory
  30. */
  31. private $formFactory;
  32. /**
  33. * @param Action\Context $context
  34. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  35. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  36. */
  37. public function __construct(
  38. Action\Context $context,
  39. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  40. \Magento\Customer\Model\Metadata\FormFactory $formFactory
  41. ) {
  42. parent::__construct($context);
  43. $this->resultJsonFactory = $resultJsonFactory;
  44. $this->formFactory = $formFactory;
  45. }
  46. /**
  47. * AJAX customer address validation action
  48. *
  49. * @return Json
  50. */
  51. public function execute(): Json
  52. {
  53. /** @var \Magento\Framework\DataObject $response */
  54. $response = new \Magento\Framework\DataObject();
  55. $response->setError(false);
  56. /** @var \Magento\Framework\DataObject $validatedResponse */
  57. $validatedResponse = $this->validateCustomerAddress($response);
  58. $resultJson = $this->resultJsonFactory->create();
  59. if ($validatedResponse->getError()) {
  60. $validatedResponse->setError(true);
  61. $validatedResponse->setMessages($response->getMessages());
  62. }
  63. $resultJson->setData($validatedResponse);
  64. return $resultJson;
  65. }
  66. /**
  67. * Customer address validation.
  68. *
  69. * @param DataObject $response
  70. * @return \Magento\Framework\DataObject
  71. */
  72. private function validateCustomerAddress(DataObject $response): DataObject
  73. {
  74. $addressForm = $this->formFactory->create('customer_address', 'adminhtml_customer_address');
  75. $formData = $addressForm->extractData($this->getRequest());
  76. $errors = $addressForm->validateData($formData);
  77. if ($errors !== true) {
  78. $messages = $response->hasMessages() ? $response->getMessages() : [];
  79. foreach ($errors as $error) {
  80. $messages[] = $error;
  81. }
  82. $response->setMessages($messages);
  83. $response->setError(true);
  84. }
  85. return $response;
  86. }
  87. }