0

fellow Magento developers. Ive been working on this new exercise including a whole admin panel and all that. i was working on the edit and ran into an issue that produces these 2 errors in my log

main.DEBUG: Request validation failed for action "Smart\Banner\Controller\Adminhtml\Banner\Edit\Interceptor" {"exception":"[object] (Magento\Framework\App\Request\InvalidRequestException(code: 0): Invalid request received at D:\Projects\mag6\vendor\magento\framework\App\Request\HttpMethodValidator.php:69)"} [] main.DEBUG: cache_invalidate: {"method":"GET","url":"http://127.0.0.1:8090/index.php/admin_1svdad/smart_banner/banner/edit/id/36/key/4dab352e3f21454a6619c5f072d8ef3cf757ec535eb6815a4459c8f7166c8664/","invalidateInfo":{"identifier":"LOCKED_RECORD_INFO_BLOCK_635c37e4d9084fb1bbdb04eaa956f5ea95c792a9"}} []

And here is all code relating to this functionality and ill try to be brief and keep the code only limited to whats relevant

This file is located in

Smart/Banner/view/adminhtml/ui_component/smart_banner_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

<argument name="data" xsi:type="array">
    <item name="js_config" xsi:type="array">
        <item name="provider" xsi:type="string">
            smart_banner_listing.smart_banner_listing_data_source
        </item>
        <item name="deps" xsi:type="string">
            smart_banner_listing.smart_banner_listing_data_source
        </item>
    </item>

    <item name="spinner" xsi:type="string">smartcoloumn</item>
    <item name="buttons" xsi:type="array">
        <item name="add" xsi:type="array">
            <item name="name" xsi:type="string">add</item>
            <item name="label" xsi:type="string" translate="true">Add New Banner</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">*/*/new</item>
        </item>
    </item>
</argument>

<dataSource name="smart_banner_listing_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">
            Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
        </argument>
        <argument name="name" xsi:type="string">smart_banner_listing_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">id</argument>
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                <item name="update_url" xsi:type="url" path="mui/index/render"/>
                <item name="storageConfig" xsi:type="array">
                    <item name="indexField" xsi:type="string">id</item>
                </item>
            </item>
        </argument>
    </argument>
</dataSource>

<listingToolbar name="listing_top">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sticky" xsi:type="boolean">true</item>
        </item>
    </argument>
    <bookmark name="bookmarks"/>
    <columnsControls name="columns_controls"/>
    <filterSearch name="fulltext"/>
    <filters name="listing_filters"/>
    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
            </item>
        </argument>
        <action name="delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="smart_banner/Banner/massDelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete Post</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to delete
                            selected items?
                        </item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>
    <paging name="listing_paging"/>
    <exportButton name="export_button"/>
</listingToolbar>

 <columns name="smartcoloumn">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">id</item>
                </item>
            </argument>
        </selectionsColumn>

     <actionsColumn name="actions" class="Smart\Banner\Ui\Component\Listing\Column\ServiceActions">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="resizeEnabled" xsi:type="boolean">false</item>
                        <item name="resizeDefaultWidth" xsi:type="string">107</item>
                        <item name="indexField" xsi:type="string">id</item>
                    </item>
                </argument>
            </actionsColumn>
        </columns>
    </listing>

AS you see my file ServiceActions is called and here is its code:

This file is located in

Smart/Banner/Ui/Component/Listing/Column/ServiceActions.php

<?php

namespace Smart\Banner\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;

class ServiceActions extends Column
{

protected $urlBuilder;

public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    UrlInterface $urlBuilder,
    array $components = [],
    array $data = []
) {
    $this->urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as &$item) {
            $item[$this->getData('name')]['edit'] = [
                'href' => $this->urlBuilder->getUrl(
                    'smart_banner/banner/edit',
                    ['id' => $item['id'],]
                ),
                'label' => __('Edit'),
                'hidden' => false,
            ];
        }
    }

    return $dataSource;
}

}

and this file calls the layout and the code for that is as follows:

This file is located in:

Smart/Banner/view/adminhtml/layout/smart_banner_banner_edit.xml

   <?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Smart\Banner\Block\Adminhtml\Edit" name="service_edit" template="Smart_Banner::edit.phtml"/>
        </referenceContainer>
    </body>
</page>

and the controller for the edit is standard:

This file is located in:

Smart/Banner/Controller/Adminhtml/Banner/Edit.php

    <?php
    namespace Smart\Banner\Controller\Adminhtml\Banner;
    
    use Magento\Backend\App\Action;
    use Magento\Backend\App\Action\Context;
    use Magento\Framework\App\Action\HttpPostActionInterface;
    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\Registry;

class Edit extends Action implements HttpPostActionInterface
{

protected $_coreRegistry;
protected $pageLayoutBuilder;


protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Smart_Banner::banner_edit');
}


public function __construct(
    Context  $context,
    Registry $registry
)
{
    $this->_coreRegistry = $registry;
    parent::__construct($context);
}


public function execute()
{
    $id = $this->getRequest()->getParam('id');
    $model = $this->_objectManager->create('Smart\Banner\Model\Banner');
    if ($id) {
        $model->load($id);
        if (!$model->getId()) {
            $this->messageManager->addError(__('This Banner no longer exists.'));
            $this->_redirect('smart/banner/*');
            return;
        }
    }
    $layout = $this->_view->loadLayout();
    $this->_view->getLayout()->initMessages();

    $this->_coreRegistry->register('smart_banner', $model);

    $title = $this->_view->getPage()->getConfig()->getTitle();
    $title->prepend(__("Banner"));

    $breadcrumb = $id ? __('Edit Banner') : __('New Banner');
    $this->_addBreadcrumb($breadcrumb, $breadcrumb);

    $this->_view->renderLayout();
} 

}

And the block file that the layout calls is this:

This file is located in

Smart/Banner/Block/Adminhtml/Edit.php

<?php

namespace Smart\Banner\Block\Adminhtml;

use Magento\Backend\Block\Widget\Context;
use Magento\Backend\Block\Widget\Form\Container;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Registry;

class Edit extends Container
{

    /**
     * Core registry
     *
     * @var Registry
     */
    protected $_coreRegistry = null;

    /**
     * @param Context $context
     * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        Context  $context,
        Registry $registry,
        array    $data = []
    )
    {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Init class
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_objectId = 'id';
        $this->_controller = 'adminhtml_banner';
        $this->_blockGroup = 'ShopForBuild_SmartBanner';

        parent::_construct();

        $this->buttonList->update('save', 'label', __('Save Banner'));
        $this->buttonList->update('delete', 'label', __('Delete Banner'));

        $this->buttonList->add(
            'save_and_continue',
            [
                'label' => __('Save and Continue Edit'),
                'class' => 'save',
                'data_attribute' => [
                    'mage-init' => ['button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form']],
                ]
            ],
            10
        );
    }
}

the layout file also calls a template file and the code for that is also very standard

This file is located in

Smart/Banner/view/adminhtml/templates/edit.phtml

<?php

use Magento\Backend\Block\Widget\Form\Container;
?>
<?php /* @escapeNotVerified */
echo $block->getFormInitScripts() ?>
<?php if ($block->getButtonsHtml('toolbar')): ?>
    <div class="page-form-actions" <?php /* @escapeNotVerified */
    echo $block->getUiId('content-header') ?>><?php echo $block->getButtonsHtml('toolbar') ?></div>
<?php endif; ?>
<?php echo $block->getFormHtml() ?>
<?php if ($block->hasFooterButtons()): ?>
    <div class="content-footer">
        <p class="form-buttons"><?php echo $block->getButtonsHtml('footer') ?></p>
    </div>
<?php endif; ?>

<script>
    require([
        'jquery',
        'mage/backend/form',
        'mage/backend/validation'
    ], function ($) {
        $('#edit_form').form()
            .validation({
                validationUrl: '<?php /* @escapeNotVerified */ echo $block->getValidationUrl() ?>',
                highlight: function (element) {
                    var detailsElement = $(element).closest('details');
                    if (detailsElement.length && detailsElement.is('.details')) {
                        var summaryElement = detailsElement.find('summary');
                        if (summaryElement.length && summaryElement.attr('aria-expanded') === "false") {
                            summaryElement.trigger('click');
                        }
                    }
                    $(element).trigger('highlight.validate');
                }
            });
    });
</script>
<?php /* @escapeNotVerified */
echo $block->getFormScripts() ?>

But after all that I seem to get the error, I mentioned above and I get this on screen enter image description here

But I don't understand what am I missing? is there something I'm not seeing? can someone help me get this edit page to work or at least show? I would greatly appreciate any help you guys can offer. Thanks in advance and Merry Christmas.

1 Answer 1

0

all I needed to do was go to the controller for the edit located in:

Smart/Banner/Controller/Adminhtml/Banner/Edit.php

and change the class it implements from POST to GET

class Edit extends Action implements HttpGetActionInterface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.