EditPresenter and SignPresenter from tutorial errors
- Cupara
- Member | 90
When following the tutorial for Create Your First Application I have two errors that I can't figure out what should go in the parentheses to solve the issue.
EditPresenter.php
protected function createComponentPostForm(): Form
{
$form = new Form;
$form->addText('title', 'Title:')
->setRequired();
$form->addTextArea('content', 'Content:')
->setRequired();
$form->addSubmit('send', 'Save and publish');
$form->onSuccess[] = $this->postFormSucceeded(...);
return $form;
}
Notice $form->onSuccess[] = $this->postFormSucceeded(...);
and how it has three dots. What do I need to put in there so it will properly
pass the data to the function?
SignPresenter.php
protected function createComponentSignInForm(): Form
{
$form = new Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addSubmit('send', 'Sign in');
$form->onSuccess[] = $this->signInFormSucceeded(...);
return $form;
}
Notice
$form->onSuccess[] = $this->signInFormSucceeded(...);
and
it's the same issue as EditPresenter.php above.
One last thing from EditPresenter.php
public function renderEdit(int $postId): void
{
$post = $this->database
->table('news')
->get($postId);
if (!$post) {
$this->error('Article not found');
}
$this->getComponent('postForm')
->setDefaults($post->toArray());
}
Notice
$this->getComponent('postForm')->setDefaults($post->toArray());
,
I get Undefined Method error for setDefaults().
Thanks for any help, it's much appreciated.
- nightfish
- Member | 517
@Cupara $this->postFormSucceeded(...)
is a first class callable syntax, which is available starting with
PHP 8.1. You are probably using an older version. All PHP versions older than
PHP 8.1 are no longer actively maintained. When starting new projects
nowadays, I would prefer PHP 8.3 or PHP 8.2 in case some of my dependencies
are not yet PHP 8.3-compatible. Nette already supports PHP 8.3.
- Cupara
- Member | 90
@nightfish I don't have the option to install PHP 8.2 or 8.3 on my server right now. I only have PHP 8.0 and PHP 8.1 installed. I'm using PHP 8.1 as the main for the entire server. Honestly, the tutorial should be updated to reflect all of this.
I'm not sure what to do to update that in this case. Here is the full file, maybe you can help me change it or whatever to complete it.
Here is my EditPresenter.php with that specific line you talked about:
<?php
namespace App\Presenters;
use Nette;
use Nette\Application\UI\Form;
final class EditPresenter extends Nette\Application\UI\Presenter
{
public function __construct(
private Nette\Database\Explorer $database,
) {
}
protected function createComponentPostForm(): Form
{
$form = new Form;
$form->addText('title', 'Title:')
->setRequired();
$form->addTextArea('content', 'Content:')
->setRequired();
$form->addSubmit('send', 'Save and publish');
$form->onSuccess[] = $this->postFormSucceeded(...);
return $form;
}
private function postFormSucceeded(array $data): void
{
$postId = $this->getParameter('postId');
if ($postId) {
$post = $this->database
->table('news')
->get($postId);
$post->update($data);
} else {
$post = $this->database
->table('news')
->insert($data);
}
$this->flashMessage('Article was published', 'success');
$this->redirect('Post:show', $post->id);
}
public function renderEdit(int $postId): void
{
$post = $this->database
->table('news')
->get($postId);
if (!$post) {
$this->error('Article not found');
}
$this->getComponent('postForm')
->setDefaults($post->toArray());
}
public function startup(): void
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in');
}
}
}
- Marek Bartoš
- Nette Blogger | 1261
You already got your answer, the three dots are first-class callable syntax.
$form->onSuccess[] = $this->postFormSucceeded(...);
means
“Call $this->postFormSucceeded()
when form is submited
and valid”
- Cupara
- Member | 90
@MarekBartoš that doesn't tell me anything. Does that mean I can just leave the three dots and it will work just fine?
What about my other error in regards to
$this->getComponent('postForm')->setDefaults($post->toArray());
where setDefaults($post->toArray());
is erroring out in VS Code
as an Undefined method 'setDefaults'
.
- Marek Bartoš
- Nette Blogger | 1261
Dots are just fine. Read the documentation for first class callables. https://www.php.net/…e_syntax.php
I don't know anything about VSCode, but both
$this->getComponent('postForm')
and
$this['postForm']
have to be supported either by IDE itself or by
some plugin in order to deduce component type from createComponent* methods.
In PHPStorm both work just fine with Nette plugin.
$this->getComponent['postForm']
this is wrong, it should be
one of the syntaxes I wrote above. They work the same way.