/** * {@inheritdoc} */ public function validate(\Magento\Catalog\Model\Product $product, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\DataObject $response) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'validate'); if (!$pluginInfo) { return parent::validate($product, $request, $response); } else { return $this->___callPlugins('validate', func_get_args(), $pluginInfo); } }
/** * Validate product * * @return \Magento\Framework\Controller\Result\Json * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { $response = new \Magento\Framework\DataObject(); $response->setError(false); try { $productData = $this->getRequest()->getPost('product', []); if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) { $productData['stock_data']['use_config_manage_stock'] = 0; } $storeId = $this->getRequest()->getParam('store', 0); $store = $this->getStoreManager()->getStore($storeId); $this->getStoreManager()->setCurrentStore($store->getCode()); /* @var $product \Magento\Catalog\Model\Product */ $product = $this->productFactory->create(); $product->setData('_edit_mode', true); if ($storeId) { $product->setStoreId($storeId); } $setId = $this->getRequest()->getPost('set') ?: $this->getRequest()->getParam('set'); if ($setId) { $product->setAttributeSetId($setId); } $typeId = $this->getRequest()->getParam('type'); if ($typeId) { $product->setTypeId($typeId); } $productId = $this->getRequest()->getParam('id'); if ($productId) { $product->load($productId); } $product = $this->getInitializationHelper()->initializeFromData($product, $productData); /* set restrictions for date ranges */ $resource = $product->getResource(); $resource->getAttribute('special_from_date')->setMaxValue($product->getSpecialToDate()); $resource->getAttribute('news_from_date')->setMaxValue($product->getNewsToDate()); $resource->getAttribute('custom_design_from')->setMaxValue($product->getCustomDesignTo()); $this->productValidator->validate($product, $this->getRequest(), $response); } catch (\Magento\Eav\Model\Entity\Attribute\Exception $e) { $response->setError(true); $response->setAttribute($e->getAttributeCode()); $response->setMessages([$e->getMessage()]); } catch (\Magento\Framework\Exception\LocalizedException $e) { $response->setError(true); $response->setMessages([$e->getMessage()]); } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); $layout = $this->layoutFactory->create(); $layout->initMessages(); $response->setError(true); $response->setHtmlMessage($layout->getMessagesBlock()->getGroupedHtml()); } return $this->resultJsonFactory->create()->setData($response); }
/** * Validate product * * @return void */ public function execute() { $response = new \Magento\Framework\Object(); $response->setError(false); try { $productData = $this->getRequest()->getPost('product'); if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) { $productData['stock_data']['use_config_manage_stock'] = 0; } /* @var $product \Magento\Catalog\Model\Product */ $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product'); $product->setData('_edit_mode', true); $storeId = $this->getRequest()->getParam('store'); if ($storeId) { $product->setStoreId($storeId); } $setId = $this->getRequest()->getParam('set'); if ($setId) { $product->setAttributeSetId($setId); } $typeId = $this->getRequest()->getParam('type'); if ($typeId) { $product->setTypeId($typeId); } $productId = $this->getRequest()->getParam('id'); if ($productId) { $product->load($productId); } $dateFieldFilters = array(); $attributes = $product->getAttributes(); foreach ($attributes as $attrKey => $attribute) { if ($attribute->getBackend()->getType() == 'datetime') { if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { $dateFieldFilters[$attrKey] = $this->_dateFilter; } } } $inputFilter = new \Zend_Filter_Input($dateFieldFilters, array(), $productData); $productData = $inputFilter->getUnescaped(); $product->addData($productData); /* set restrictions for date ranges */ $resource = $product->getResource(); $resource->getAttribute('special_from_date')->setMaxValue($product->getSpecialToDate()); $resource->getAttribute('news_from_date')->setMaxValue($product->getNewsToDate()); $resource->getAttribute('custom_design_from')->setMaxValue($product->getCustomDesignTo()); $this->productValidator->validate($product, $this->getRequest(), $response); } 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->addError($e->getMessage()); $this->_view->getLayout()->initMessages(); $response->setError(true); $response->setHtmlMessage($this->_view->getLayout()->getMessagesBlock()->getGroupedHtml()); } $this->getResponse()->representJson($response->toJson()); }