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?
appcodeDevRestApiBlockAdminhtmlTokenEditForm.php
<?php
namespace DevRestApiBlockAdminhtmlTokenEdit;
use MagentoBackendBlockWidgetFormGeneric;
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();
}
}
appcodeDevRestApiControllerAdminhtmlTokenSave.php
<?php
namespace DevRestApiControllerAdminhtmlToken;
use MagentoBackendAppAction;
use DevRestApiModelTokenFactory;
class Save extends Action
{
protected $tokenFactory;
public function __construct(
ActionContext $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');
}
}
appcodeDevRestApiviewadminhtmllayoutdev_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="DevRestApiBlockAdminhtmlTokenEdit" name="token_edit" />
</referenceContainer>
</body>
</page>