Exemple #1
0
 /**
  * Attributes validation action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object();
     $response->setError(false);
     $attributesData = $this->getRequest()->getParam('attributes', array());
     $data = new \Magento\Framework\Object();
     try {
         if ($attributesData) {
             foreach ($attributesData as $attributeCode => $value) {
                 $attribute = $this->_objectManager->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', $attributeCode);
                 if (!$attribute->getAttributeId()) {
                     unset($attributesData[$attributeCode]);
                     continue;
                 }
                 $data->setData($attributeCode, $value);
                 $attribute->getBackend()->validate($data);
             }
         }
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $e) {
         $response->setError(true);
         $response->setAttribute($e->getAttributeCode());
         $response->setMessage($e->getMessage());
     } catch (\Magento\Framework\Model\Exception $e) {
         $response->setError(true);
         $response->setMessage($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('Something went wrong while updating the product(s) attributes.'));
         $this->_view->getLayout()->initMessages();
         $response->setError(true);
         $response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml());
     }
     $this->getResponse()->representJson($response->toJson());
 }
 /**
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object();
     $response->setError(false);
     $attributeCode = $this->getRequest()->getParam('attribute_code');
     $frontendLabel = $this->getRequest()->getParam('frontend_label');
     $attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
     $attributeId = $this->getRequest()->getParam('attribute_id');
     $attribute = $this->_objectManager->create('Magento\\Catalog\\Model\\Resource\\Eav\\Attribute')->loadByCode($this->_entityTypeId, $attributeCode);
     if ($attribute->getId() && !$attributeId) {
         if (strlen($this->getRequest()->getParam('attribute_code'))) {
             $response->setAttributes(['attribute_code' => __('An attribute with this code already exists.')]);
         } else {
             $response->setAttributes(['attribute_label' => __('Attribute with the same code (%1) already exists.', $attributeCode)]);
         }
         $response->setError(true);
     }
     if ($this->getRequest()->has('new_attribute_set_name')) {
         $setName = $this->getRequest()->getParam('new_attribute_set_name');
         /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
         $attributeSet = $this->_objectManager->create('Magento\\Eav\\Model\\Entity\\Attribute\\Set');
         $attributeSet->setEntityTypeId($this->_entityTypeId)->load($setName, 'attribute_set_name');
         if ($attributeSet->getId()) {
             $setName = $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($setName);
             $this->messageManager->addError(__('Attribute Set with name \'%1\' already exists.', $setName));
             $layout = $this->layoutFactory->create();
             $layout->initMessages();
             $response->setError(true);
             $response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml());
         }
     }
     return $this->resultJsonFactory->create()->setJsonData($response->toJson());
 }
Exemple #3
0
 /**
  * AJAX customer validation action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object();
     $response->setError(0);
     $errors = null;
     $userId = (int) $this->getRequest()->getParam('user_id');
     $data = $this->getRequest()->getPost();
     try {
         /** @var $model \Magento\User\Model\User */
         $model = $this->_userFactory->create()->load($userId);
         $model->setData($this->_getAdminUserData($data));
         $errors = $model->validate();
     } catch (\Magento\Framework\Model\Exception $exception) {
         /* @var $error Error */
         foreach ($exception->getMessages(\Magento\Framework\Message\MessageInterface::TYPE_ERROR) as $error) {
             $errors[] = $error->getText();
         }
     }
     if ($errors !== true && !empty($errors)) {
         foreach ($errors as $error) {
             $this->messageManager->addError($error);
         }
         $response->setError(1);
         $this->_view->getLayout()->initMessages();
         $response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml());
     }
     $this->getResponse()->representJson($response->toJson());
 }
Exemple #4
0
 /**
  * Validate Action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object(array('error' => false));
     $variable = $this->_initVariable();
     $variable->addData($this->getRequest()->getPost('variable'));
     $result = $variable->validate();
     if ($result !== true && is_string($result)) {
         $this->messageManager->addError($result);
         $this->_view->getLayout()->initMessages();
         $response->setError(true);
         $response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml());
     }
     $this->getResponse()->representJson($response->toJson());
 }
Exemple #5
0
 /**
  * Validate action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object();
     $response->setError(false);
     $widgetInstance = $this->_initWidgetInstance();
     $result = $widgetInstance->validate();
     if ($result !== true && is_string($result)) {
         $this->messageManager->addError($result);
         $this->_view->getLayout()->initMessages();
         $response->setError(true);
         $response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml());
     }
     $response = $response->toJson();
     $this->_translateInline->processResponseBody($response);
     $this->_response->representJson($response);
 }
Exemple #6
0
 /**
  * Validate Action
  *
  * @return \Magento\Framework\Controller\Result\JSON
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object(['error' => false]);
     $variable = $this->_initVariable();
     $variable->addData($this->getRequest()->getPost('variable'));
     $result = $variable->validate();
     if ($result !== true && is_string($result)) {
         $this->messageManager->addError($result);
         $layout = $this->layoutFactory->create();
         $layout->initMessages();
         $response->setError(true);
         $response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml());
     }
     /** @var \Magento\Framework\Controller\Result\JSON $resultJson */
     $resultJson = $this->resultJsonFactory->create();
     return $resultJson->setData($response->toArray());
 }
 /**
  * Validate Action
  *
  * @return \Magento\Framework\Controller\Result\Json
  */
 public function execute()
 {
     $response = new \Magento\Framework\Object(['error' => false]);
     $webhook = $this->_initWebhook();
     $webhook->addData($this->getRequest()->getPost('webhook'));
     $result = $webhook->validate();
     if ($result instanceof \Magento\Framework\Phrase) {
         $this->messageManager->addError($result->getText());
         $layout = $this->layoutFactory->create();
         $layout->initMessages();
         $response->setError(true);
         $response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml());
     }
     /** @var \Magento\Framework\Controller\Result\Json $resultJson */
     $resultJson = $this->resultJsonFactory->create();
     return $resultJson->setData($response->toArray());
 }