zend framework 3 tutorial

3.5k Views Asked by At

I am trying a Zend framework 3 tutorial and am getting stuck in "editing" a function in the in-depth part (Blog case).

When trying to edit a blog message, the editing form doesn't show the original message. It seems that the original message couldn't be bound to the form.

I copied all the sample code. I don't know what is wrong with it. By the way, my add and delete function work fine.

can anyone help me with it?

The editAction method from the tutorial:

public function editAction()
{
    $id = $this->params()->fromRoute('id');
    if (! $id) {
        return $this->redirect()->toRoute('blog');
    }
    try {
        $post = $this->repository->findPost($id);
    } catch (InvalidArgumentException $ex) {
        return $this->redirect()->toRoute('blog');
    }
    $this->form->bind($post);
    $viewModel = new ViewModel(['form' => $this->form]);
    $request = $this->getRequest();
    if (! $request->isPost()) {
        return $viewModel;
    }
    $this->form->setData($request->getPost());
    if (! $this->form->isValid()) {
        return $viewModel;
    }
    $post = $this->command->updatePost($post);
    return $this->redirect()->toRoute(
        'blog/detail',
        ['id' => $post->getId()]
    );
}
1

There are 1 best solutions below

0
On

Edit this code:

if (! $request->isPost()) {
    foreach($this->form->getMessages() as $message){
        $this->flashMessenger()->addErrorMessage($message['message']);
    }
}

In your view:

<?php echo $this->flashMessenger()->renderCurrent('error', ['options go here...']); ?>