redirect after form submit from other class

AndiLeni
Member | 8
+
0
-

Hello,

I have trouble figuring out how to successfully make a redirect after my form submit.
What I have is my class FolderPresenter which renders the website and creates the form:

protected function createComponentNewfolderForm(): Form
	{
		$form = new Form;
		$form->addText('name', 'Name:')
			->setRequired();

		$form->addSubmit('send', 'Erstellen');
		$form->onSuccess[] = [new FileManagerPresenter, 'newfolderFormSucceeded'];

		return $form;
	}

As you can see, the creation of the folder should happen in the other class FileManagerPresenter, as this class should also handle other file operations.

public function newfolderFormSucceeded(Form $form, array $values) {

        // $this->forward('Folder:show', '/');
        $this->redirect('Folder:show', '/');
}

Unfiortunately, this forward or redirect response give me this error:
Argument 1 passed to Nette\Application\Helpers::splitName() must be of the type string, null given, called in C:\laragon\www\tinycloud\vendor\nette\application\src\Application\UI\Presenter.php on line 798
I can not understand why this error is raised, since redirects work fine in other parts.

Might this issue be caused due to the fact that I try to use another classes method?
I am thankfull for any hint or tips if my way is unaesthetic and can be done more easily.

Thanks and kind regards
Andreas

Rick Strafy
Nette Blogger | 65
+
0
-

Hi, that's the not the best approach, you have created a new instance of FileManagerPresenter, you need the instance from DI container.
But anyway, presenters can't (or at least shouldn't) interact with each other, you need some abstract BaseFile presenter that both presenters will extend and give that method/forms there.

Try to look at https://github.com/…at-file-blog and how it's designed, because creation of the folder shouldn't be responsibility of any presenter, it should be in Model layer (some FileManager, and you can inject that class via constructor to other presenters, like that https://github.com/…resenter.php#L20), as you can see ArticleFacade in that example, it's business logic of the app and it shouldn't be in controller layer, registration of that service is in /config/model.neon.

dakur
Member | 493
+
+2
-

@AndiLeni I don't know from your code snippet where createComponentNewfolderForm() and newfolderFormSucceeded() are placed, but I guess there are one after another in the same presenter. So you don't need to create new instance of the presenter but use the one you are in. So basically changing:

$form->onSuccess[] = [new FileManagerPresenter, 'newfolderFormSucceeded'];

to:

$form->onSuccess[] = [$this, 'newfolderFormSucceeded'];

should be enough.

And if they are not, then you need to have this onSuccess listener in the same file. Why would you have it in separate file?

Last edited by dakur (2021-07-16 06:55)