Jump to content

Custom module with admin controller - Invalid Security Token


Recommended Posts

Hello,

I write custom module to upload image file and i have problem when i have send file. I receive:

Quote

Invalid Security Token

My AdminUploadImageController

<?php

class AdminImageUploadController extends ModuleAdminController
{
    /** @var string  */
    private $name = 'imageuploader';

    public function __construct()
    {
        $this->bootstrap = true;
        parent::__construct();
    }

    public function initContent()
    {
        parent::initContent();
        $this->context->smarty->assign(array('content' => $this->displayForm()));
    }

    public function displayForm()
    {
        // Get default language
        $defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');

        // Init Fields form array
        $fieldsForm[0]['form'] = [
            'legend' => [
                'title' => $this->l('Upload image'),
            ],
            'input' => [
                [
                    'type' => 'file',
                    'label' => $this->l('Choose file'),
                    'name' => 'image',
                    'multiple' => false,
                    'required' => true
                ]
            ],
            'submit' => [
                'title' => $this->l('Save file'),
                'class' => 'btn btn-default pull-right'
            ]
        ];

        $helper = new HelperForm();

        // Module, token and currentIndex

        $helper->module = $this;
        $helper->name_controller = "AdminImageUpload";
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex . '&configure='.$this->name;

        // Language
        $helper->default_form_language = $defaultLang;
        $helper->allow_employee_form_lang = $defaultLang;

        // Title and toolbar
        $helper->title = $this->displayName;
        $helper->show_toolbar = false;        // false -> remove toolbar
        $helper->toolbar_scroll = false;      // yes - > Toolbar is always visible on the top of the screen.
        $helper->submit_action = 'save';


        return $helper->generateForm($fieldsForm);
    }

    public function postProcess()
    {
        if (Tools::isSubmit('save')) {
            echo "ok";
        }
        parent::postProcess();
    }
}

Where is the mistake that makes me have a bad security token?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...