Ejemplo n.º 1
0
 /**
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $response = new \Magento\Framework\DataObject();
     $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\\ResourceModel\\Eav\\Attribute')->loadByCode($this->_entityTypeId, $attributeCode);
     if ($attribute->getId() && !$attributeId) {
         if (strlen($this->getRequest()->getParam('attribute_code'))) {
             $response->setMessage(__('An attribute with this code already exists.'));
         } else {
             $response->setMessage(__('An 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(__('An attribute set named \'%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());
 }
Ejemplo n.º 2
0
 /**
  * AJAX customer validation action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\DataObject();
     $response->setError(0);
     $errors = null;
     $userId = (int) $this->getRequest()->getParam('user_id');
     $data = $this->getRequest()->getPostValue();
     try {
         /** @var $model \Magento\User\Model\User */
         $model = $this->_userFactory->create()->load($userId);
         $model->setData($this->_getAdminUserData($data));
         $errors = $model->validate();
     } 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 !== 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());
 }
Ejemplo n.º 3
0
 /**
  * Validate action
  *
  * @return void
  */
 public function execute()
 {
     $response = new \Magento\Framework\DataObject();
     $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);
 }
Ejemplo n.º 4
0
 /**
  * Validate Action
  *
  * @return \Magento\Framework\Controller\Result\Json
  */
 public function execute()
 {
     $response = new \Magento\Framework\DataObject(['error' => false]);
     $variable = $this->_initVariable();
     $variable->addData($this->getRequest()->getPost('variable'));
     $result = $variable->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());
 }