1

In the admin side, I have a template file in which I have a form submission.

On form submission, an action would be executed, but it is redirecting to dashboard.

can anyone have any idea, how to overcome this kind of problem in magento 2

  • This problem occurs mainly due to an invalid action url given in the form. Please check your form action and make sure your action controller is executed. Put a die('hit the controller action') inside your controller action. – MGento Oct 24 '17 at 5:12
  • i have controller for action but it is not even called when form submitted @MGento – Learing_Coder Oct 24 '17 at 5:21
2

Forgot to add form key to the form:

<input name="form_key" type="hidden" value="<?php /* @escapeNotVerified */ echo $block->getFormKey(); ?>" />

Or

<?php echo $block->getBlockHtml('formkey')?>
  • But in block i should have getFormKey() in order to use in my template file, – Learing_Coder Oct 24 '17 at 5:21
  • it have used in admin backend setting secure url Off to formkey @Khoa TruongDinh – Learing_Coder Oct 24 '17 at 5:23
  • @PranayK we need to add the form key to form. – Khoa TruongDinh Oct 24 '17 at 5:24
  • it have used in admin backend setting secure url Off to formkey: where is this option config? – Khoa TruongDinh Oct 24 '17 at 5:24
  • Thanks It is Working @Khoa TruongDinh after placing this in template <?php echo $block->getBlockHtml('formkey')?> – Learing_Coder Oct 24 '17 at 5:58
1

write code in your block

public function __construct(

    \Magento\Backend\Block\Template\Context $context  

    array $data = array()
    ) {
    $this->formKey = $context->getFormKey();

    parent::__construct($context, $data);
}

public function getFormKey() {

        return $this->formKey->getFormKey();
}
  • it is throwing an error – Learing_Coder Oct 24 '17 at 5:54
  • please provide me error in more detail also clear var/generation and check. – Pratik Oct 24 '17 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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