I have below code
app\code\Custom\Module\Controller\MyModule\PostForm.php
namespace Custom\Module\Controller\MyModule;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Filesystem\DirectoryList;
class PostForm extends \Magento\Framework\App\Action\Action {
/**
* @var DataPersistorInterface
*/
private $dataPersistor;
/** @var \Magento\Framework\View\Result\Page */
protected $resultPageFactory;
/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
protected $inlineTranslation;
/**
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
protected $_formKeyValidator;
/** * @param \Magento\Framework\App\Action\Context $context */
public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation) {
$this->resultPageFactory = $resultPageFactory;
$this->_formKeyValidator = $formKeyValidator;
$this->inlineTranslation = $inlineTranslation;
parent::__construct($context);
}
/**
* Post user post
*
* @return void
* @throws \Exception
*/
public function execute() {
$resultPage = $this->resultPageFactory->create();
$resultRedirect = $this->resultRedirectFactory->create();
$post = $this->getRequest()->getPostValue();
if (!$post) {
$this->_redirect('*/*/');
return;
}
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $resultRedirect->setPath('*/*/');
}
$this->inlineTranslation->suspend();
$postObject = new \Magento\Framework\DataObject();
$postObject->setData($post);
$error = false;
$errors = [];
if (!\Zend_Validate::is(trim($post['title']), 'NotEmpty')) {
$errors[] = __('Please enter title.');
$error = true;
}
if (!\Zend_Validate::is(trim($post['description']), 'NotEmpty')) {
$errors[] = __('Please enter description.');
$error = true;
}
if ($error) {
foreach ($errors as $error) {
$this->messageManager->addErrorMessage($error);
}
$this->getDataPersistor()->set('mymodule_post', $post);
return $resultRedirect->setPath('module/mymodule/new/');
}
}
/**
* Get Data Persistor
*
* @return DataPersistorInterface
*/
public function getDataPersistor() {
if ($this->dataPersistor === null) {
$this->dataPersistor = ObjectManager::getInstance()
->get(DataPersistorInterface::class);
}
return $this->dataPersistor;
}
}
What is purpose of dataPersistor
& when i got error my data are not filled up in Form. I have filled up Title, but Description is blank.
So when error message display, it should display my filled up Title.