I'm trying to save a simple phtml form with a single input field. Can't figure why clicking on 'Save' Button won't trigger a call to the save.php controller. Path seems ok, but no action is triggered.
Code below:
Yotpo\Yotpo\Block\Adminhtml\System\index.php:
namespace Yotpo\Yotpo\Block\Adminhtml\System;
class Index extends \Magento\Backend\Block\Widget\Form\Container {
protected $_blockGroup = 'Yotpo_Yotpo';
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
\Yotpo\Yotpo\Block\Config $config,
array $data = []
) {
$this->_config = $config;
parent::__construct($context, $data);
}
protected function _construct()
{
$this->_blockGroup = 'Yotpo_Yotpo';
$this->_controller = 'adminhtml_Settings_index';
parent::_construct();
}
protected function _prepareLayout()
{
$this->getToolbar()->addChild(
'save_button',
'Magento\Backend\Block\Widget\Button',
[
'id' => 'save',
'label' => __('Save Configuration'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'save', 'target' => '#yotpo-form'],
]
]
]
);
}
public function getFormActionUrl()
{
return $this->getUrl('adminhtml/settings/save');
}
}
Yotpo\Yotpo\Controller\Adminhtml\Settings\save.php:
namespace Yotpo\Yotpo\Controller\Adminhtml\System;
class SaveRates extends \Magento\Backend\App\Action
{
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Yotpo\Yotpo\Block\Config $config,
array $data = []
) {
$this->_config = $config;
parent::__construct($context, $data);
}
public function execute()
{
$this->_redirect('adminhtml/*/');
}
}
Yotpo\Yotpo\View\Adminhtml\Layout\adminhtml_settings_index.xml:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<update handle="formkey" />
<body>
<referenceContainer name="content">
<block class="Yotpo\Yotpo\Block\Adminhtml\System\index" name="yotpo.settings" template="grid.phtml"/>
</referenceContainer>
</body>
</page>
Yotpo\Yotpo\View\Adminhtml\Templates\grid.phtml:
?>
<?php
/**
* @var $block \Yotpo\Yoypo\Block\Adminhtml\System\index
*/
?>
<form id="yotpo-form" action="<?php echo $block->getFormActionUrl() ?>" method="post">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>" />
<input id="app_key" type="text" value="<?php echo $block->_config->getAppKey();?>" class="
</form>
clicking on 'Save Configuration' button suppose to trigger the save action.
if I add a simple html submit button – this button triggers the desired save action:
<input type="submit" value="submit">
How can I make the Magento save button to work?
Would appreciate your help!
Best Answer
Based on your description your problem is with the Magento-added save button connecting to your form. I can't comment on that. But there are other problems with the code you've provided:
Please verify you copied the right code and filenames. If so, there are some mismatches between filename and class/namespace that are probably causing your problems.
Yotpo\Yotpo\Block\Adminhtml\System\index.php
: All class filenames should start with an uppercase letter, exactly matching the contained class name. This should beIndex.php
. Fix references accordingly.Yotpo\Yotpo\Controller\Adminhtml\Settings\save.php
: Again, filename capitalization.Save.php
. There's also a complete discrepancy between that file path and your namespace and class name. Based on that path, your namespace should be:Yotpo\Yotpo\Controller\Adminhtml\Settings
. Your class name should be:Save
. What you have contains System (rather than Settings), and SaveRates (rather than Save).<input id="app_key" type="text" value="<?php echo $block->_config->getAppKey();?>" class="
(line ends there)