0

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 Rakuten\MemberCard\Controller\Adminhtml\Card\Save->execute() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#2 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#3 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#4 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->___callPlugins() called at [generated/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save/Interceptor.php:23]
#5 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:111]
#6 Magento\Framework\App\Action\Action->dispatch() called at [vendor/magento/module-backend/App/AbstractAction.php:151]
#7 Magento\Backend\App\AbstractAction->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#8 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#9 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-backend/App/Action/Plugin/Authentication.php:145]
#10 Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#11 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#12 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->___callPlugins() called at [generated/code/Rakuten/MemberCard/Controller/Adminhtml/Card/Save/Interceptor.php:32]
#13 Rakuten\MemberCard\Controller\Adminhtml\Card\Save\Interceptor->dispatch() called at [vendor/magento/framework/App/FrontController.php:245]
#14 Magento\Framework\App\FrontController->getActionResponse() called at [vendor/magento/framework/App/FrontController.php:212]
#15 Magento\Framework\App\FrontController->processRequest() called at [vendor/magento/framework/App/FrontController.php:147]
#16 Magento\Framework\App\FrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#17 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#18 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#19 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:23]
#20 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [vendor/magento/framework/App/Http.php:116]
#21 Magento\Framework\App\Http->launch() called at [vendor/magento/framework/App/Bootstrap.php:264]
#22 Magento\Framework\App\Bootstrap->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');
    }
4
  • what does it mean, could you explain to me ?
    – KAITO_3SS
    Commented Jul 22, 2022 at 4:44
  • how can i get the value ?
    – KAITO_3SS
    Commented Jul 22, 2022 at 4:49
  • ok, i can fix it now it all start from my admin form.xml, i did not change the data scope
    – KAITO_3SS
    Commented Jul 22, 2022 at 5:02
  • Please see the outcome if key value coming or not: $this->getRequest()->getParams();
    – Dotsquares
    Commented Jul 22, 2022 at 5:11

3 Answers 3

0

Check whether your $data contains the array key card_pin

 echo "<pre>";
 print_r($data);
 exit;

paste this code after this line

$data = $this->getRequest()->getPostValue();

in your execute function

2
  • So where did you set this from admin form make sure your field name should have same name i.e. card_pin, you can check this by displaying data did your post array contains card_pin or not . Commented Jul 22, 2022 at 4:55
  • Can you please share your code from admin form where you have created this field 'card_pin' Commented Jul 22, 2022 at 4:57
0

enter image description hereok so i do add like you recommend and i see it already has it. You can check the image below

0
0

try to do this like

 if (isset($data["card_pin"])) {
     //paste your code here
 }
 else {
      $data['card_pin'] = null;
 }

Did you check Line No 36 ? Is It this one

 $newData = [
        **'card_pin' => $data['card_pin']**,
        'status' => $data['status'],
        'content' => $data['content'],
    ];

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.