I've created a custom form in my backend.

I can access it via /admin/export/export/index.

But when I submit my form, Magento is redirecting me to my admin home page. My form needs only to have 1 button to trigger the export of some data.

See my phtml :

<form method="POST" action="<?php echo $block->getUrl('export/export/index')?>">
    <input type="hidden" name="action" value="export">
    <button type="submit">Exporter les données</button>

and my controler :

    public function execute()
    if('POST' == $this->getRequest()->getMethod()){ //not going inside 


1 Answer 1


You are getting this error because all of the post requests need to have a valid form_key value sent.

try adding this inside your form tag.

<?= $block->getLayout()->getBlock('form_key');?>

BUt I'm not sure if that works for admin forms.

If that does not work, try this:
add this in the block that renders the form

private $formKey;

public function __construct(
    \Magento\Framework\Data\Form\FormKey $formKey,
) {
    $this->formKey = $formKey;

public function getFormKey()
    return $this->formKey->getFormKey();

Now you can add this to your form

<input type="hidden" name="form_key" value="<?= $block->getFormKey();?>" />
  • Yes, that's it ! Aug 9, 2019 at 15:55
  • which one? I haven't tested any of them. The short one or the long one?
    – Marius
    Aug 9, 2019 at 16:08
  • Not tested yet, but I've tried to add the key in ajax form (by getting the key thourhg the url), and the ajax is working (I'll let you know which one is working) Aug 12, 2019 at 8:50

Your Answer

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

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