Skip to content

Exception #0 (Exception): Warning: Undefined array key

I have this error after i try to save data from my admin grid. How to fix this error.

Exception #0 (Exception): Warning: Undefined array key "card_pin" in /var/www/html/rakuten_magento/app/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save.php on line 36

Exception #0 (Exception): Warning: Undefined array key "card_pin" in /var/www/html/rakuten_magento/app/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save.php on line 36
<pre>#1 RakutenMemberCardControllerAdminhtmlCardSave->execute() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#2 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#3 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->MagentoFrameworkInterception{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#4 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->___callPlugins() called at [generated/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save/Interceptor.php:23]
#5 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:111]
#6 MagentoFrameworkAppActionAction->dispatch() called at [vendor/magento/module-backend/App/AbstractAction.php:151]
#7 MagentoBackendAppAbstractAction->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#8 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#9 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->MagentoFrameworkInterception{closure}() called at [vendor/magento/module-backend/App/Action/Plugin/Authentication.php:145]
#10 MagentoBackendAppActionPluginAuthentication->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#11 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->MagentoFrameworkInterception{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#12 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->___callPlugins() called at [generated/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save/Interceptor.php:32]
#13 RakutenMemberCardControllerAdminhtmlCardSaveInterceptor->dispatch() called at [vendor/magento/framework/App/FrontController.php:245]
#14 MagentoFrameworkAppFrontController->getActionResponse() called at [vendor/magento/framework/App/FrontController.php:212]
#15 MagentoFrameworkAppFrontController->processRequest() called at [vendor/magento/framework/App/FrontController.php:147]
#16 MagentoFrameworkAppFrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#17 MagentoFrameworkAppFrontControllerInterceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#18 MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterception{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#19 MagentoFrameworkAppFrontControllerInterceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:23]
#20 MagentoFrameworkAppFrontControllerInterceptor->dispatch() called at [vendor/magento/framework/App/Http.php:116]
#21 MagentoFrameworkAppHttp->launch() called at [vendor/magento/framework/App/Bootstrap.php:264]
#22 MagentoFrameworkAppBootstrap->run() called at [pub/index.php:30]
</pre>

Here is my controller Save file:

public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        $id = !empty($data['entity_id']) ? $data['entity_id'] : null;
 
        $newData = [
            'card_pin' => $data['card_pin'],
            'status' => $data['status'],
            'content' => $data['content'],
        ];
 
        $post = $this->postFactory->create();
 
        if ($id) {
            $post->load($id);
        }
        try {
            $post->addData($newData);
            $post->save();
            $this->messageManager->addSuccessMessage(__('You saved the post.'));
        } catch (Exception $e) {
            $this->messageManager->addErrorMessage(__($e->getMessage()));
        }
 
        return $this->resultRedirectFactory->create()->setPath('helloworld/card/index');
    }