My situation:
I created a new token_table (with InstallSchema.php) to store the token that will be needed when sending an API request to my endpoint.
Now I managed to create a form where admin user can enter the token and save it.
But after clicking on the "Save" button I get the following:
Can anyone suggest what the problem might be?
app\code\Dev\RestApi\Block\Adminhtml\Token\Edit\Form.php
<?php
namespace Dev\RestApi\Block\Adminhtml\Token\Edit;
use Magento\Backend\Block\Widget\Form\Generic;
class Form extends Generic
{
protected function _prepareForm()
{
$form = $this->_formFactory->create(
[
'data' => [
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post'
],
]
);
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Token Information')]
);
$fieldset->addField(
'token',
'text',
[
'name' => 'token',
'label' => __('Token'),
'title' => __('Token'),
'required' => true,
]
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
app\code\Dev\RestApi\Controller\Adminhtml\Token\Save.php
<?php
namespace Dev\RestApi\Controller\Adminhtml\Token;
use Magento\Backend\App\Action;
use Dev\RestApi\Model\TokenFactory;
class Save extends Action
{
protected $tokenFactory;
public function __construct(
Action\Context $context,
TokenFactory $tokenFactory
) {
parent::__construct($context);
$this->tokenFactory = $tokenFactory;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
if ($data) {
try {
$tokenModel = $this->tokenFactory->create();
$tokenModel->setData($data);
$tokenModel->save();
$this->messageManager->addSuccessMessage(__('Token has been saved.'));
$this->_redirect('*/*/index');
return;
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
}
app\code\Dev\RestApi\view\adminhtml\layout\dev_controller_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="page.title">
<action method="setPageTitle">
<argument name="title" xsi:type="string">Module</argument>
</action>
</referenceBlock>
<body>
<referenceContainer name="content">
<block class="Dev\RestApi\Block\Adminhtml\Token\Edit" name="token_edit" />
</referenceContainer>
</body>
</page>