XF 2.3 Stuck at Building a form - Building with XenForo 2 part 7

Anatoliy

Well-known member
When I hit the Save button while adding a new note, I'm getting the error

Code:
TypeError: Demo\Pad\Pub\Controller\Note::actionSave(): Argument #1 ($params) must be of type Demo\Pad\Pub\Controller\ParameterBag, XF\Mvc\ParameterBag given, called in /Applications/XAMPP/xamppfiles/htdocs/src/XF/Mvc/Dispatcher.php on line 362 in src/addons/Demo/Pad/Pub/Controller/Note.php at line 55
Demo\Pad\Pub\Controller\Note->actionSave() in src/XF/Mvc/Dispatcher.php at line 362
XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 264
XF\Mvc\Dispatcher->dispatchFromMatch() in src/XF/Mvc/Dispatcher.php at line 121
XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 63
XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2813
XF\App->run() in src/XF.php at line 802
XF::runApp() in index.php at line 23

I watched videos several times but failed to figure out what's wrong. Here's my controller.

PHP:
<?php
namespace Demo\Pad\Pub\Controller;
use XF\Pub\Controller\AbstractController;
class Note extends AbstractController
{
    public function actionIndex()
    {
        return $this->view('Demo\Pad:Note\Index', 'demo_pad_index');
    }
    public function actionTest()
    {
        // $userFinder = $this->finder('XF/User')
        // ->where('user_id', '<', 4);
        // $total = $userFinder->total();
        // $users = $userFinder->fetch();
        // $note = $this->em()->create('Demo\Pad:Note');
        // $note->title = 'First note title';
        // $note->content = 'First nnote content...';
        // $note->save();
        $viewParams = [
        ];
        return $this->view('Demo\Pad:Note\Test', 'demo_pad_test', $viewParams);
    }
    public function actionAdd()
    {
        $note = $this->em()->create('Demo\Pad:Note');
        return $this->noteAddEdit($note);
    }
    public function actionEdit(ParameterBag $params)
    {
        $note = $this->assertNotedExist($params->note_id);
        return $this->noteAddEdit($note);
    }
    protected function noteAddEdit(\Demo\Pad\Entity\Note $note)
    {
        $viewParams = [
            'note' => $note
        ];
        return $this->view('Demo\Pad:Note\Edit', 'demo_pad_edit', $viewParams);
    }
    public function actionSave(ParameterBag $params)
    {
        if($params->note_id)
        {
            $note = $this->assertNotedExist($params->note_id);
        }
        else
        {
            $note = $this->em()->create('Demo\Pad:Note');
        }
        $this->noteSaveProcess($note)->run();
        return $this->redirect($this->buildLink('notes'));
    }
    protected function noteSaveProcess(\Demo\Pad\Entity\Note $note)
    {
        $input = $this->filter([
            'title' => 'str',
            'content' => 'str'
        ]);
        $form = $this->formAction();
        $form->basicEntitySave($note, $input);
        return $form;
    }
    protected function assertNoteExist($id, $with = null, $phraseKey = null)
    {
        return $this->assertRecordExist('Demo\Pad:Note', $id, $with, $phraseKey);
    }
}

Please help.
 
Try this instead
use XF\Mvc\ParameterBag; added.
PHP:
<?php
namespace Demo\Pad\Pub\Controller;

use XF\Pub\Controller\AbstractController;
use XF\Mvc\ParameterBag;

class Note extends AbstractController
{
    public function actionIndex()
    {
        return $this->view('Demo\Pad:Note\Index', 'demo_pad_index');
    }

    public function actionTest()
    {
        // Example of how to fetch users and create a note (commented out for now)
        // $userFinder = $this->finder('XF/User')->where('user_id', '<', 4);
        // $total = $userFinder->total();
        // $users = $userFinder->fetch();
        // $note = $this->em()->create('Demo\Pad:Note');
        // $note->title = 'First note title';
        // $note->content = 'First note content...';
        // $note->save();

        $viewParams = [];
        return $this->view('Demo\Pad:Note\Test', 'demo_pad_test', $viewParams);
    }

    public function actionAdd()
    {
        $note = $this->em()->create('Demo\Pad:Note');
        return $this->noteAddEdit($note);
    }

    public function actionEdit(ParameterBag $params)
    {
        $note = $this->assertNoteExist($params->note_id);
        return $this->noteAddEdit($note);
    }

    protected function noteAddEdit(\Demo\Pad\Entity\Note $note)
    {
        $viewParams = [
            'note' => $note
        ];
        return $this->view('Demo\Pad:Note\Edit', 'demo_pad_edit', $viewParams);
    }

    public function actionSave(ParameterBag $params)
    {
        if ($params->note_id) {
            $note = $this->assertNoteExist($params->note_id);
        } else {
            $note = $this->em()->create('Demo\Pad:Note');
        }

        $this->noteSaveProcess($note)->run();
        return $this->redirect($this->buildLink('notes'));
    }

    protected function noteSaveProcess(\Demo\Pad\Entity\Note $note)
    {
        $input = $this->filter([
            'title' => 'str',
            'content' => 'str'
        ]);

        $form = $this->formAction();
        $form->basicEntitySave($note, $input);
        return $form;
    }

    protected function assertNoteExist($id, $with = null, $phraseKey = null)
    {
        return $this->assertRecordExist('Demo\Pad:Note', $id, $with, $phraseKey);
    }
}
 
Damn it! )))
Thanks! Now it works.

So what's the deal? VSCode supposed to add
Code:
use XF\Mvc\ParameterBag;
for me? or the tutorial missed this part?
 
It breaks my heart but we have to face the facts, AI is a powerful and fast tool... It has just solved your problem in a few seconds, without that you could have waited 1 hour, 5 hours, 1 day or more... It's unfortunate but that's how it is. And what's more, it doesn't just give you a result, it explains your mistakes...
 
Damn it! )))
Thanks! Now it works.

So what's the deal? VSCode supposed to add
Code:
use XF\Mvc\ParameterBag;
for me? or the tutorial missed this part?
There's an extension you can install for auto namespacing/imports for PHP. VS Code by itself won't do that for you.
 
It breaks my heart but we have to face the facts, AI is a powerful and fast tool... It has just solved your problem in a few seconds, without that you could have waited 1 hour, 5 hours, 1 day or more... It's unfortunate but that's how it is. And what's more, it doesn't just give you a result, it explains your mistakes...
There is no shame in this. I've used it before as well. And it's easier to write it yourself, do research for countless hours.
 
Back
Top Bottom